home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / custom / wid-edit.el.z / wid-edit.el
Encoding:
Text File  |  1998-05-21  |  120.2 KB  |  3,653 lines

  1. ;;; wid-edit.el --- Functions for creating and using widgets.
  2. ;;
  3. ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  6. ;; Maintainer: Hrvoje Niksic <hniksic@srce.hr>
  7. ;; Keywords: extensions
  8. ;; Version: 1.9960-x
  9. ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
  10.  
  11. ;; This file is part of XEmacs.
  12.  
  13. ;; XEmacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; XEmacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;; GNU General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with XEmacs; see the file COPYING.  If not, write to the
  25. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. ;; Boston, MA 02111-1307, USA.
  27.  
  28. ;;; Commentary:
  29. ;;
  30. ;; See `widget.el'.
  31.  
  32.  
  33. ;;; Code:
  34.  
  35. (require 'widget)
  36.  
  37. (autoload 'pp-to-string "pp")
  38. (autoload 'finder-commentary "finder" nil t)
  39.  
  40. ;;; Customization.
  41.  
  42. (defgroup widgets nil
  43.   "Customization support for the Widget Library."
  44.   :link '(custom-manual "(widget)Top")
  45.   :link '(url-link :tag "Development Page"
  46.            "http://www.dina.kvl.dk/~abraham/custom/")
  47.   :link '(emacs-library-link :tag "Lisp File" "widget.el")
  48.   :prefix "widget-"
  49.   :group 'extensions
  50.   :group 'hypermedia)
  51.  
  52. (defgroup widget-documentation nil
  53.   "Options controling the display of documentation strings."
  54.   :group 'widgets)
  55.  
  56. (defgroup widget-faces nil
  57.   "Faces used by the widget library."
  58.   :group 'widgets
  59.   :group 'faces)
  60.  
  61. (defvar widget-documentation-face 'widget-documentation-face
  62.   "Face used for documentation strings in widges.
  63. This exists as a variable so it can be set locally in certain buffers.")
  64.  
  65. (defface widget-documentation-face '((((class color)
  66.                        (background dark))
  67.                       (:foreground "lime green"))
  68.                      (((class color)
  69.                        (background light))
  70.                       (:foreground "dark green"))
  71.                      (t nil))
  72.   "Face used for documentation text."
  73.   :group 'widget-documentation
  74.   :group 'widget-faces)
  75.  
  76. (defvar widget-button-face 'widget-button-face
  77.   "Face used for buttons in widges.
  78. This exists as a variable so it can be set locally in certain buffers.")
  79.  
  80. (defface widget-button-face '((t (:bold t)))
  81.   "Face used for widget buttons."
  82.   :group 'widget-faces)
  83.  
  84. (defcustom widget-mouse-face 'highlight
  85.   "Face used for widget buttons when the mouse is above them."
  86.   :type 'face
  87.   :group 'widget-faces)
  88.  
  89. (defface widget-field-face '((((class grayscale color)
  90.                    (background light))
  91.                   (:background "gray85"))
  92.                  (((class grayscale color)
  93.                    (background dark))
  94.                   (:background "dim gray"))
  95.                  (t
  96.                   (:italic t)))
  97.   "Face used for editable fields."
  98.   :group 'widget-faces)
  99.  
  100. ;; Currently unused
  101. ;(defface widget-single-line-field-face '((((class grayscale color)
  102. ;                       (background light))
  103. ;                      (:background "gray85"))
  104. ;                     (((class grayscale color)
  105. ;                       (background dark))
  106. ;                      (:background "dim gray"))
  107. ;                     (t
  108. ;                      (:italic t)))
  109. ;  "Face used for editable fields spanning only a single line."
  110. ;  :group 'widget-faces)
  111. ;
  112. ;(defvar widget-single-line-display-table
  113. ;  (let ((table (make-display-table)))
  114. ;    (aset table 9  "^I")
  115. ;    (aset table 10 "^J")
  116. ;    table)
  117. ;  "Display table used for single-line editable fields.")
  118. ;
  119. ;(set-face-display-table 'widget-single-line-field-face
  120. ;            widget-single-line-display-table)
  121.  
  122.  
  123. ;; Some functions from this file have been ported to C for speed.
  124. ;; Setting this to t (*before* loading wid-edit.el) will make them
  125. ;; shadow the subrs.  It should be used only for debugging purposes.
  126. (defvar widget-shadow-subrs nil)
  127.  
  128.  
  129. ;;; Utility functions.
  130. ;;
  131. ;; These are not really widget specific.
  132.  
  133. (when (or (not (fboundp 'widget-plist-member))
  134.       widget-shadow-subrs)
  135.   ;; Recoded in C, for efficiency.  It used to be a defsubst, but old
  136.   ;; compiled code won't fail -- it will just be slower.
  137.   (defun widget-plist-member (plist prop)
  138.     ;; Return non-nil if PLIST has the property PROP.
  139.     ;; PLIST is a property list, which is a list of the form
  140.     ;; (PROP1 VALUE1 PROP2 VALUE2 ...).  PROP is a symbol.
  141.     ;; Unlike `plist-get', this allows you to distinguish between a missing
  142.     ;; property and a property with the value nil.
  143.     ;; The value is actually the tail of PLIST whose car is PROP.
  144.     (while (and plist (not (eq (car plist) prop)))
  145.       (setq plist (cddr plist)))
  146.     plist))
  147.  
  148. (defun widget-princ-to-string (object)
  149.   ;; Return string representation of OBJECT, any Lisp object.
  150.   ;; No quoting characters are used; no delimiters are printed around
  151.   ;; the contents of strings.
  152.   (with-current-buffer (get-buffer-create " *widget-tmp*")
  153.     (erase-buffer)
  154.     (princ object (current-buffer))
  155.     (buffer-string)))
  156.  
  157. (defun widget-clear-undo ()
  158.   "Clear all undo information."
  159.   (buffer-disable-undo)
  160.   (buffer-enable-undo))
  161.  
  162. (defcustom widget-menu-max-size 40
  163.   "Largest number of items allowed in a popup-menu.
  164. Larger menus are read through the minibuffer."
  165.   :group 'widgets
  166.   :type 'integer)
  167.  
  168. (defcustom widget-menu-minibuffer-flag nil
  169.   "*Control how to ask for a choice from the keyboard.
  170. Non-nil means use the minibuffer;
  171. nil means read a single character."
  172.   :group 'widgets
  173.   :type 'boolean)
  174.  
  175. (defun widget-choose (title items &optional event)
  176.   "Choose an item from a list.
  177.  
  178. First argument TITLE is the name of the list.
  179. Second argument ITEMS is an list whose members are either
  180.  (NAME . VALUE), to indicate selectable items, or just strings to
  181.  indicate unselectable items.
  182. Optional third argument EVENT is an input event.
  183.  
  184. The user is asked to choose between each NAME from the items alist,
  185. and the VALUE of the chosen element will be returned.  If EVENT is a
  186. mouse event, and the number of elements in items is less than
  187. `widget-menu-max-size', a popup menu will be used, otherwise the
  188. minibuffer."
  189.   (cond    ((and (< (length items) widget-menu-max-size)
  190.           event
  191.           (console-on-window-system-p))
  192.      ;; Pressed by the mouse.
  193.      (let ((val (get-popup-menu-response
  194.              (cons title
  195.                (mapcar (lambda (x)
  196.                      (if (stringp x)
  197.                      (vector x nil nil)
  198.                        (vector (car x) (list (car x)) t)))
  199.                    items)))))
  200.        (setq val (and val
  201.               (listp (event-object val))
  202.               (stringp (car-safe (event-object val)))
  203.               (car (event-object val))))
  204.        (cdr (assoc val items))))
  205.     ((and (not widget-menu-minibuffer-flag)
  206.           ;; Can't handle more than 10 items (as many digits)
  207.           (<= (length items) 10))
  208.      ;; Construct a menu of the choices
  209.      ;; and then use it for prompting for a single character.
  210.      (let* ((overriding-terminal-local-map (make-sparse-keymap))
  211.         (map (make-sparse-keymap title))
  212.         (next-digit ?0)
  213.         some-choice-enabled value)
  214.        ;; Define SPC as a prefix char to get to this menu.
  215.        (define-key overriding-terminal-local-map " " map)
  216.        (with-current-buffer (get-buffer-create " widget-choose")
  217.          (erase-buffer)
  218.          (insert "Available choices:\n\n")
  219.          (dolist (choice items)
  220.            (when (consp choice)
  221.          (let* ((name (car choice))
  222.             (function (cdr choice)))
  223.            (insert (format "%c = %s\n" next-digit name))
  224.            (define-key map (vector next-digit) function)
  225.            (setq some-choice-enabled t)))
  226.            ;; Allocate digits to disabled alternatives
  227.            ;; so that the digit of a given alternative never varies.
  228.            (incf next-digit))
  229.          (insert "\nC-g = Quit"))
  230.        (or some-choice-enabled
  231.            (error "None of the choices is currently meaningful"))
  232.        (define-key map [?\C-g] 'keyboard-quit)
  233.        (define-key map [t] 'keyboard-quit)
  234.        ;(setcdr map (nreverse (cdr map)))
  235.        ;; Unread a SPC to lead to our new menu.
  236.        (push (character-to-event ?\ ) unread-command-events)
  237.        ;; Read a char with the menu, and return the result
  238.        ;; that corresponds to it.
  239.        (save-window-excursion
  240.          (display-buffer (get-buffer " widget-choose"))
  241.          (let ((cursor-in-echo-area t))
  242.            (setq value
  243.              (lookup-key overriding-terminal-local-map
  244.                  (read-key-sequence (concat title ": ") t)))))
  245.        (message "")
  246.        (when (or (eq value 'keyboard-quit)
  247.              (null value))
  248.          (error "Canceled"))
  249.        value))
  250.     (t
  251.      ;; Read the choice of name from the minibuffer.
  252.      (setq items (remove-if 'stringp items))
  253.      (let ((val (completing-read (concat title ": ") items nil t)))
  254.        (if (stringp val)
  255.            (let ((try (try-completion val items)))
  256.          (when (stringp try)
  257.            (setq val try))
  258.          (cdr (assoc val items)))
  259.          nil)))))
  260.  
  261.  
  262. ;;; Widget text specifications.
  263. ;;
  264. ;; These functions are for specifying text properties.
  265.  
  266. (defcustom widget-field-add-space t
  267.   ;; Setting this to nil might be available, once some problems are resolved.
  268.   "Non-nil means add extra space at the end of editable text fields.
  269.  
  270. This is needed on all versions of Emacs.  If you don't add the space,
  271. it will become impossible to edit a zero size field."
  272.   :type 'boolean
  273.   :group 'widgets)
  274.  
  275. (defcustom widget-field-use-before-change
  276.   (and (or (> emacs-minor-version 34)
  277.        (> emacs-major-version 19))
  278.        (not (string-match "XEmacs" emacs-version)))
  279.   "Non-nil means use `before-change-functions' to track editable fields.
  280. This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
  281. Using before hooks also means that the :notify function can't know the
  282. new value."
  283.   :type 'boolean
  284.   :group 'widgets)
  285.  
  286. (defun widget-specify-field (widget from to)
  287.   "Specify editable button for WIDGET between FROM and TO."
  288.   (save-excursion
  289.     (goto-char to)
  290.     (cond ((null (widget-get widget :size))
  291.        (forward-char 1))
  292.       ;; Terminating space is not part of the field, but necessary in
  293.       ;; order for local-map to work.  Remove next sexp if local-map works
  294.       ;; at the end of the extent.
  295.       (widget-field-add-space
  296.        (insert-and-inherit " ")))
  297.     (setq to (point)))
  298.   (let ((map (widget-get widget :keymap))
  299.     (face (or (widget-get widget :value-face) 'widget-field-face))
  300.     (help-echo (widget-get widget :help-echo))
  301.     (extent (make-extent from to)))
  302.     (unless (or (stringp help-echo) (null help-echo))
  303.       (setq help-echo 'widget-mouse-help))
  304.     (widget-put widget :field-extent extent)
  305.     (and (or (not widget-field-add-space)
  306.          (widget-get widget :size))
  307.      (set-extent-property extent 'end-closed nil))
  308.     (set-extent-property extent 'detachable nil)
  309.     (set-extent-property extent 'field widget)
  310.     (set-extent-property extent 'button-or-field t)
  311.     (set-extent-property extent 'keymap map)
  312.     (set-extent-property extent 'face face)
  313.     (set-extent-property extent 'balloon-help help-echo)
  314.     (set-extent-property extent 'help-echo help-echo)))
  315.  
  316. (defun widget-specify-button (widget from to)
  317.   "Specify button for WIDGET between FROM and TO."
  318.   (let ((face (widget-apply widget :button-face-get))
  319.     (help-echo (widget-get widget :help-echo))
  320.     (extent (make-extent from to))
  321.     (map (widget-get widget :button-keymap)))
  322.     (widget-put widget :button-extent extent)
  323.     (unless (or (null help-echo) (stringp help-echo))
  324.       (setq help-echo 'widget-mouse-help))
  325.     (set-extent-property extent 'start-open t)
  326.     (set-extent-property extent 'button widget)
  327.     (set-extent-property extent 'button-or-field t)
  328.     (set-extent-property extent 'mouse-face widget-mouse-face)
  329.     (set-extent-property extent 'balloon-help help-echo)
  330.     (set-extent-property extent 'help-echo help-echo)
  331.     (set-extent-property extent 'face face)
  332.     (set-extent-property extent 'keymap map)))
  333.  
  334. (defun widget-mouse-help (extent)
  335.   "Find mouse help string for button in extent."
  336.   (let* ((widget (widget-at (extent-start-position extent)))
  337.      (help-echo (and widget (widget-get widget :help-echo))))
  338.     (cond ((stringp help-echo)
  339.        help-echo)
  340.       ((and (functionp help-echo)
  341.         (stringp (setq help-echo (funcall help-echo widget))))
  342.        help-echo)
  343.       (t
  344.        (format "(widget %S :help-echo %S)" widget help-echo)))))
  345.  
  346. (defun widget-specify-sample (widget from to)
  347.   ;; Specify sample for WIDGET between FROM and TO.
  348.   (let ((face (widget-apply widget :sample-face-get))
  349.     (extent (make-extent from to nil)))
  350.     (set-extent-property extent 'start-open t)
  351.     (set-extent-property extent 'face face)
  352.     (widget-put widget :sample-extent extent)))
  353.  
  354. (defun widget-specify-doc (widget from to)
  355.   ;; Specify documentation for WIDGET between FROM and TO.
  356.   (let ((extent (make-extent from to)))
  357.     (set-extent-property extent 'start-open t)
  358.     (set-extent-property extent 'widget-doc widget)
  359.     (set-extent-property extent 'face widget-documentation-face)
  360.     (widget-put widget :doc-extent extent)))
  361.  
  362. (defmacro widget-specify-insert (&rest form)
  363.   ;; Execute FORM without inheriting any text properties.
  364.   `(save-restriction
  365.      (let ((inhibit-read-only t)
  366.        before-change-functions
  367.        after-change-functions)
  368.        (insert "<>")
  369.        (narrow-to-region (- (point) 2) (point))
  370.        (goto-char (1+ (point-min)))
  371.        ;; We use `prog1' instead of a `result' variable, as the latter
  372.        ;; confuses the byte-compiler in some cases (a warning).
  373.        (prog1 (progn ,@form)
  374.      (delete-region (point-min) (1+ (point-min)))
  375.      (delete-region (1- (point-max)) (point-max))
  376.      (goto-char (point-max))))))
  377.  
  378. (put 'widget-specify-insert 'edebug-form-spec '(&rest form))
  379.  
  380.  
  381. ;;; Inactive Widgets.
  382.  
  383. (defface widget-inactive-face '((((class grayscale color)
  384.                   (background dark))
  385.                  (:foreground "light gray"))
  386.                 (((class grayscale color)
  387.                   (background light))
  388.                  (:foreground "dim gray"))
  389.                 (t
  390.                  (:italic t)))
  391.   "Face used for inactive widgets."
  392.   :group 'widget-faces)
  393.  
  394. ;; For inactiveness to work on complex structures, it is not
  395. ;; sufficient to keep track of whether a button/field/glyph is
  396. ;; inactive or not -- we must know how many time it was deactivated
  397. ;; (inactiveness level).  Successive deactivations of the same button
  398. ;; increment its inactive-count, and activations decrement it.  When
  399. ;; inactive-count reaches 0, the button/field/glyph is reactivated.
  400.  
  401. (defun widget-activation-widget-mapper (extent action)
  402.   "Activate or deactivate EXTENT's widget (button or field).
  403. Suitable for use with `map-extents'."
  404.   (ecase action
  405.     (:activate
  406.      (decf (extent-property extent :inactive-count))
  407.      (when (zerop (extent-property extent :inactive-count))
  408.        (set-extent-properties
  409.     extent (extent-property extent :inactive-plist))
  410.        (set-extent-property extent :inactive-plist nil)))
  411.     (:deactivate
  412.      (incf (extent-property extent :inactive-count 0))
  413.      ;; Store a plist of old properties, which will be fed to
  414.      ;; `set-extent-properties'.
  415.      (unless (extent-property extent :inactive-plist)
  416.        (set-extent-property
  417.     extent :inactive-plist
  418.     (list 'mouse-face (extent-property extent 'mouse-face)
  419.           'help-echo (extent-property extent 'help-echo)
  420.           'keymap (extent-property extent 'keymap)))
  421.        (set-extent-properties
  422.     extent '(mouse-face nil help-echo nil keymap nil)))))
  423.   nil)
  424.  
  425. (defun widget-activation-glyph-mapper (extent action)
  426.   (let ((activate-p (if (eq action :activate) t nil)))
  427.     (if activate-p
  428.     (decf (extent-property extent :inactive-count))
  429.       (incf (extent-property extent :inactive-count 0)))
  430.     (when (or (and activate-p
  431.            (zerop (extent-property extent :inactive-count)))
  432.           (and (not activate-p)
  433.            (not (zerop (extent-property extent :inactive-count)))))
  434.       (let* ((glyph-widget (extent-property extent 'glyph-widget))
  435.          (up-glyph (widget-get glyph-widget :glyph-up))
  436.          (inactive-glyph (widget-get glyph-widget :glyph-inactive))
  437.          (new-glyph (if activate-p up-glyph inactive-glyph)))
  438.      ;; Check that the new glyph exists, and differs from the
  439.      ;; default one.
  440.     (and up-glyph inactive-glyph (not (eq up-glyph inactive-glyph))
  441.          ;; Check if the glyph is already installed.
  442.          (not (eq (extent-end-glyph extent) new-glyph))
  443.          ;; Change it.
  444.          (set-extent-end-glyph extent new-glyph)))))
  445.   nil)
  446.  
  447. (defun widget-specify-inactive (widget from to)
  448.   "Make WIDGET inactive for user modifications."
  449.   (unless (widget-get widget :inactive)
  450.     (let ((extent (make-extent from to)))
  451.       ;; It is no longer necessary for the extent to be read-only, as
  452.       ;; the inactive editable fields now lose their keymaps.
  453.       (set-extent-properties
  454.        extent '(start-open t face widget-inactive-face
  455.         detachable t priority 2001 widget-inactive t))
  456.       (widget-put widget :inactive extent))
  457.     ;; Deactivate the buttons and fields within the range.  In some
  458.     ;; cases, the fields are not yet setup at the time this function
  459.     ;; is called.  Those fields are deactivated explicitly by
  460.     ;; `widget-setup'.
  461.     (map-extents 'widget-activation-widget-mapper
  462.          nil from to :deactivate nil 'button-or-field)
  463.     ;; Deactivate glyphs.
  464.     (map-extents 'widget-activation-glyph-mapper
  465.          nil from to :deactivate nil 'glyph-widget)))
  466.  
  467. (defun widget-specify-active (widget)
  468.   "Make WIDGET active for user modifications."
  469.   (let ((inactive (widget-get widget :inactive)))
  470.     (when inactive
  471.       ;; Reactivate the buttons and fields covered by the extent.
  472.       (map-extents 'widget-activation-widget-mapper
  473.            inactive nil nil :activate nil 'button-or-field)
  474.       ;; Reactivate the glyphs.
  475.       (map-extents 'widget-activation-glyph-mapper
  476.            inactive nil nil :activate nil 'end-glyph)
  477.       (delete-extent inactive)
  478.       (widget-put widget :inactive nil))))
  479.  
  480.  
  481. ;;; Widget Properties.
  482.  
  483. (defsubst widget-type (widget)
  484.   "Return the type of WIDGET, a symbol."
  485.   (car widget))
  486.  
  487. (when (or (not (fboundp 'widget-put))
  488.       widget-shadow-subrs)
  489.   (defun widget-put (widget property value)
  490.     "In WIDGET set PROPERTY to VALUE.
  491. The value can later be retrived with `widget-get'."
  492.     (setcdr widget (plist-put (cdr widget) property value))))
  493.  
  494. ;; Recoded in C, for efficiency:
  495. (when (or (not (fboundp 'widget-get))
  496.       widget-shadow-subrs)
  497.   (defun widget-get (widget property)
  498.     "In WIDGET, get the value of PROPERTY.
  499. The value could either be specified when the widget was created, or
  500. later with `widget-put'."
  501.     (let ((missing t)
  502.       value tmp)
  503.       (while missing
  504.     (cond ((setq tmp (widget-plist-member (cdr widget) property))
  505.            (setq value (car (cdr tmp))
  506.              missing nil))
  507.           ((setq tmp (car widget))
  508.            (setq widget (get tmp 'widget-type)))
  509.           (t
  510.            (setq missing nil))))
  511.       value)))
  512.  
  513. (defun widget-get-indirect (widget property)
  514.   "In WIDGET, get the value of PROPERTY.
  515. If the value is a symbol, return its binding.
  516. Otherwise, just return the value."
  517.   (let ((value (widget-get widget property)))
  518.     (if (symbolp value)
  519.     (symbol-value value)
  520.       value)))
  521.  
  522. (defun widget-member (widget property)
  523.   "Non-nil iff there is a definition in WIDGET for PROPERTY."
  524.   (cond ((widget-plist-member (cdr widget) property)
  525.      t)
  526.     ((car widget)
  527.      (widget-member (get (car widget) 'widget-type) property))
  528.     (t nil)))
  529.  
  530. (when (or (not (fboundp 'widget-apply))
  531.       widget-shadow-subrs)
  532.   ;;This is in C, so don't ###utoload
  533.   (defun widget-apply (widget property &rest args)
  534.     "Apply the value of WIDGET's PROPERTY to the widget itself.
  535. ARGS are passed as extra arguments to the function."
  536.     (apply (widget-get widget property) widget args)))
  537.  
  538. (defun widget-value (widget)
  539.   "Extract the current value of WIDGET."
  540.   (widget-apply widget
  541.         :value-to-external (widget-apply widget :value-get)))
  542.  
  543. (defun widget-value-set (widget value)
  544.   "Set the current value of WIDGET to VALUE."
  545.   (widget-apply widget
  546.         :value-set (widget-apply widget
  547.                      :value-to-internal value)))
  548.  
  549. (defun widget-match-inline (widget vals)
  550.   ;; In WIDGET, match the start of VALS.
  551.   (cond ((widget-get widget :inline)
  552.      (widget-apply widget :match-inline vals))
  553.     ((and vals
  554.           (widget-apply widget :match (car vals)))
  555.      (cons (list (car vals)) (cdr vals)))
  556.     (t nil)))
  557.  
  558. (defun widget-apply-action (widget &optional event)
  559.   "Apply :action in WIDGET in response to EVENT."
  560.   (if (widget-apply widget :active)
  561.       (widget-apply widget :action event)
  562.     (error "Attempt to perform action on inactive widget")))
  563.  
  564.  
  565. ;;; Helper functions.
  566. ;;
  567. ;; These are widget specific.
  568.  
  569. ;;;###autoload
  570. (defun widget-prompt-value (widget prompt &optional value unbound)
  571.   "Prompt for a value matching WIDGET, using PROMPT.
  572. The current value is assumed to be VALUE, unless UNBOUND is non-nil."
  573.   (unless (listp widget)
  574.     (setq widget (list widget)))
  575.   (setq prompt (format "[%s] %s" (widget-type widget) prompt))
  576.   (setq widget (widget-convert widget))
  577.   (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
  578.     (unless (widget-apply widget :match answer)
  579.       (error "Value does not match %S type." (car widget)))
  580.     answer))
  581.  
  582. (defun widget-get-sibling (widget)
  583.   "Get the item WIDGET is assumed to toggle.
  584. This is only meaningful for radio buttons or checkboxes in a list."
  585.   (let* ((parent (widget-get widget :parent))
  586.      (children (widget-get parent :children))
  587.      child)
  588.     (catch 'child
  589.       (while children
  590.     (setq child (car children)
  591.           children (cdr children))
  592.     (when (eq (widget-get child :button) widget)
  593.       (throw 'child child)))
  594.       nil)))
  595.  
  596. (defun widget-map-buttons (function &optional buffer maparg)
  597.   "Map FUNCTION over the buttons in BUFFER.
  598. FUNCTION is called with the arguments WIDGET and MAPARG.
  599.  
  600. If FUNCTION returns non-nil, the walk is cancelled.
  601.  
  602. The arguments MAPARG, and BUFFER default to nil and (current-buffer),
  603. respectively."
  604.   (map-extents (lambda (extent ignore)
  605.          ;; If FUNCTION returns non-nil, we bail out
  606.          (funcall function (extent-property extent 'button) maparg))
  607.            nil nil nil nil nil
  608.            'button))
  609.  
  610.  
  611. ;;; Glyphs.
  612.  
  613. (defcustom widget-glyph-directory (locate-data-directory "custom")
  614.   "Where widget glyphs are located.
  615. If this variable is nil, widget will try to locate the directory
  616. automatically."
  617.   :group 'widgets
  618.   :type 'directory)
  619.  
  620. (defcustom widget-glyph-enable t
  621.   "If non nil, use glyphs in images when available."
  622.   :group 'widgets
  623.   :type 'boolean)
  624.  
  625. (defcustom widget-image-conversion
  626.   '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
  627.     (xbm ".xbm"))
  628.   "Conversion alist from image formats to file name suffixes."
  629.   :group 'widgets
  630.   :type '(repeat (cons :format "%v"
  631.                (symbol :tag "Image Format" unknown)
  632.                (repeat :tag "Suffixes"
  633.                    (string :format "%v")))))
  634.  
  635. (defvar widget-glyph-cache nil
  636.   "Cache of glyphs associated with strings (files).")
  637.  
  638. (defun widget-glyph-find (image tag)
  639.   "Create a glyph corresponding to IMAGE with string TAG as fallback.
  640. IMAGE can already be a glyph, or a file name sans extension (xpm,
  641.  xbm, gif, jpg, or png) located in `widget-glyph-directory', or
  642.  in one of the data directories.
  643. It can also be a valid image instantiator, in which case it will be
  644.  used to make the glyph, with an additional TAG string fallback."
  645.   (cond ((not (and image widget-glyph-enable))
  646.      ;; We don't want to use glyphs.
  647.      nil)
  648.     ((and (not (console-on-window-system-p))
  649.           ;; We don't use glyphs on TTY consoles, although we
  650.           ;; could.  However, glyph faces aren't yet working
  651.           ;; properly, and movement through glyphs is unintuitive.
  652.           ;; As an exception, when TAG is nil, we assume that the
  653.           ;; caller knows what he is doing, and that the tag is
  654.           ;; encoded within the glyph.
  655.           (not (glyphp image)))
  656.      nil)
  657.     ((glyphp image)
  658.      ;; Already a glyph.  Use it.
  659.      image)
  660.     ((stringp image)
  661.      ;; A string.  Look it up in the cache first...
  662.      (or (lax-plist-get widget-glyph-cache image)
  663.          ;; ...and then in the relevant directories
  664.          (let* ((dirlist (cons (or widget-glyph-directory
  665.                        (locate-data-directory "custom"))
  666.                    data-directory-list))
  667.             (formats widget-image-conversion)
  668.             file)
  669.            (while (and formats (not file))
  670.          ;; This dance is necessary, because XEmacs signals an
  671.          ;; error when it encounters an unrecognized image
  672.          ;; format.
  673.          (when (valid-image-instantiator-format-p (caar formats))
  674.            (setq file (locate-file image dirlist
  675.                        (mapconcat 'identity (cdar formats)
  676.                               ":"))))
  677.          (unless file
  678.            (pop formats)))
  679.            (when file
  680.          ;; We create a glyph with the file as the default image
  681.          ;; instantiator, and the TAG fallback
  682.          (let ((glyph (make-glyph `([,(caar formats) :file ,file]
  683.                         [string :data ,tag]))))
  684.            ;; Cache the glyph
  685.            (laxputf widget-glyph-cache image glyph)
  686.            ;; ...and return it
  687.            glyph)))))
  688.     ((valid-instantiator-p image 'image)
  689.      ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
  690.      (make-glyph `(,image [string :data ,tag])))
  691.     (t
  692.      ;; Oh well.
  693.      nil)))
  694.  
  695. (defun widget-glyph-insert (widget tag image &optional down inactive)
  696.   "In WIDGET, insert the text TAG or, if supported, IMAGE.
  697. IMAGE should either be a glyph, an image instantiator, an image file
  698. name sans extension (xpm, xbm, gif, jpg, or png) located in
  699. `widget-glyph-directory', or anything else allowed by
  700. `widget-glyph-find'.
  701.  
  702. If IMAGE is a list, it will be taken as a list of (UP DOWN INACTIVE)
  703. glyphs.  The down and inactive glyphs are shown when glyph is pressed
  704. or inactive, respectively.
  705.  
  706. The optional DOWN and INACTIVE arguments are deprecated, and exist
  707. only because of compatibility."
  708.   ;; Convert between IMAGE being a list, etc.  Must use `psetq',
  709.   ;; because otherwise change to `image' screws up the rest.
  710.   (psetq image (or (and (consp image)
  711.             (car image))
  712.            image)
  713.      down (or (and (consp image)
  714.                (nth 1 image))
  715.           down)
  716.      inactive (or (and (consp image)
  717.                (nth 2 image))
  718.               inactive))
  719.   (let ((glyph (widget-glyph-find image tag)))
  720.     (if glyph
  721.     (widget-glyph-insert-glyph widget glyph
  722.                    (widget-glyph-find down tag)
  723.                    (widget-glyph-find inactive tag))
  724.       (insert tag))
  725.     glyph))
  726.  
  727. (defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
  728.   "In WIDGET, insert GLYPH.
  729. If optional arguments DOWN and INACTIVE are given, they should be
  730. glyphs used when the widget is pushed and inactive, respectively."
  731.   (insert "*")
  732.   (let ((extent (make-extent (point) (1- (point))))
  733.     (help-echo (and widget (widget-get widget :help-echo)))
  734.     (map (and widget (widget-get widget :button-keymap))))
  735.     (set-extent-property extent 'glyph-widget widget)
  736.     ;; It would be fun if we could make this extent atomic, so it
  737.     ;; doesn't mess with cursor motion.  But atomic-extents library is
  738.     ;; currently a mess, so I'd rather not use it.
  739.     (set-extent-property extent 'invisible t)
  740.     (set-extent-property extent 'start-open t)
  741.     (set-extent-property extent 'end-open t)
  742.     (set-extent-property extent 'keymap map)
  743.     (set-extent-end-glyph extent glyph)
  744.     (unless (or (stringp help-echo) (null help-echo))
  745.       (setq help-echo 'widget-mouse-help))
  746.     (when help-echo
  747.       (set-extent-property extent 'balloon-help help-echo)
  748.       (set-extent-property extent 'help-echo help-echo)))
  749.   (when widget
  750.     (widget-put widget :glyph-up glyph)
  751.     (when down (widget-put widget :glyph-down down))
  752.     (when inactive (widget-put widget :glyph-inactive inactive))))
  753.  
  754.  
  755. ;;; Buttons.
  756.  
  757. (defgroup widget-button nil
  758.   "The look of various kinds of buttons."
  759.   :group 'widgets)
  760.  
  761. (defcustom widget-button-prefix ""
  762.   "String used as prefix for buttons."
  763.   :type 'string
  764.   :group 'widget-button)
  765.  
  766. (defcustom widget-button-suffix ""
  767.   "String used as suffix for buttons."
  768.   :type 'string
  769.   :group 'widget-button)
  770.  
  771.  
  772. ;;; Creating Widgets.
  773.  
  774. ;;;###autoload
  775. (defun widget-create (type &rest args)
  776.   "Create widget of TYPE.
  777. The optional ARGS are additional keyword arguments."
  778.   (let ((widget (apply 'widget-convert type args)))
  779.     (widget-apply widget :create)
  780.     widget))
  781.  
  782. (defun widget-create-child-and-convert (parent type &rest args)
  783.   "As part of the widget PARENT, create a child widget TYPE.
  784. The child is converted, using the keyword arguments ARGS."
  785.   (let ((widget (apply 'widget-convert type args)))
  786.     (widget-put widget :parent parent)
  787.     (unless (widget-get widget :indent)
  788.       (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
  789.                     (or (widget-get widget :extra-offset) 0)
  790.                     (widget-get parent :offset))))
  791.     (widget-apply widget :create)
  792.     widget))
  793.  
  794. (defun widget-create-child (parent type)
  795.   "Create widget of TYPE."
  796.   (let ((widget (copy-sequence type)))
  797.     (widget-put widget :parent parent)
  798.     (unless (widget-get widget :indent)
  799.       (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
  800.                     (or (widget-get widget :extra-offset) 0)
  801.                     (widget-get parent :offset))))
  802.     (widget-apply widget :create)
  803.     widget))
  804.  
  805. (defun widget-create-child-value (parent type value)
  806.   "Create widget of TYPE with value VALUE."
  807.   (let ((widget (copy-sequence type)))
  808.     (widget-put widget :value (widget-apply widget :value-to-internal value))
  809.     (widget-put widget :parent parent)
  810.     (unless (widget-get widget :indent)
  811.       (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
  812.                     (or (widget-get widget :extra-offset) 0)
  813.                     (widget-get parent :offset))))
  814.     (widget-apply widget :create)
  815.     widget))
  816.  
  817. ;;;###autoload
  818. (defun widget-delete (widget)
  819.   "Delete WIDGET."
  820.   (widget-apply widget :delete))
  821.  
  822. (defun widget-convert (type &rest args)
  823.   "Convert TYPE to a widget without inserting it in the buffer.
  824. The optional ARGS are additional keyword arguments."
  825.   ;; Don't touch the type.
  826.   (let* ((widget (if (symbolp type)
  827.              (list type)
  828.            (copy-sequence type)))
  829.      (current widget)
  830.      (keys args))
  831.     ;; First set the :args keyword.
  832.     (while (cdr current)        ;Look in the type.
  833.       (let ((next (car (cdr current))))
  834.     (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
  835.         (setq current (cdr (cdr current)))
  836.       (setcdr current (list :args (cdr current)))
  837.       (setq current nil))))
  838.     (while args                ;Look in the args.
  839.       (let ((next (nth 0 args)))
  840.     (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
  841.         (setq args (nthcdr 2 args))
  842.       (widget-put widget :args args)
  843.       (setq args nil))))
  844.     ;; Then Convert the widget.
  845.     (setq type widget)
  846.     (while type
  847.       (let ((convert-widget (plist-get (cdr type) :convert-widget)))
  848.     (if convert-widget
  849.         (setq widget (funcall convert-widget widget))))
  850.       (setq type (get (car type) 'widget-type)))
  851.     ;; Finally set the keyword args.
  852.     (while keys
  853.       (let ((next (nth 0 keys)))
  854.     (if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
  855.         (progn
  856.           (widget-put widget next (nth 1 keys))
  857.           (setq keys (nthcdr 2 keys)))
  858.       (setq keys nil))))
  859.     ;; Convert the :value to internal format.
  860.     (if (widget-member widget :value)
  861.     (let ((value (widget-get widget :value)))
  862.       (widget-put widget
  863.               :value (widget-apply widget :value-to-internal value))))
  864.     ;; Return the newly created widget.
  865.     widget))
  866.  
  867. (defun widget-insert (&rest args)
  868.   "Call `insert' with ARGS and make the text read only."
  869.   (let ((inhibit-read-only t)
  870.     before-change-functions
  871.     after-change-functions)
  872.     (apply 'insert args)))
  873.  
  874. (defun widget-convert-text (type from to
  875.                  &optional button-from button-to
  876.                  &rest args)
  877.   "Return a widget of type TYPE with endpoint FROM TO.
  878. Optional ARGS are extra keyword arguments for TYPE.
  879. and TO will be used as the widgets end points. If optional arguments
  880. BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
  881. button end points.
  882. Optional ARGS are extra keyword arguments for TYPE."
  883.   (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
  884.     (from (copy-marker from))
  885.     (to (copy-marker to)))
  886.     (set-marker-insertion-type from t)
  887.     (set-marker-insertion-type to nil)
  888.     (widget-put widget :from from)
  889.     (widget-put widget :to to)
  890.     (when button-from
  891.       (widget-specify-button widget button-from button-to))
  892.     widget))
  893.  
  894. (defun widget-convert-button (type from to &rest args)
  895.   "Return a widget of type TYPE with endpoint FROM TO.
  896. Optional ARGS are extra keyword arguments for TYPE.
  897. No text will be inserted to the buffer, instead the text between FROM
  898. and TO will be used as the widgets end points, as well as the widgets
  899. button end points."
  900.   (apply 'widget-convert-text type from to from to args))
  901.  
  902. (defun widget-leave-text (widget)
  903.   "Remove markers and extents from WIDGET and its children."
  904.   (let ((from (widget-get widget :from))
  905.     (to (widget-get widget :to))
  906.     (button (widget-get widget :button-extent))
  907.     (sample (widget-get widget :sample-extent))
  908.     (doc (widget-get widget :doc-extent))
  909.     (field (widget-get widget :field-extent))
  910.     (children (widget-get widget :children)))
  911.     (set-marker from nil)
  912.     (set-marker to nil)
  913.     ;; Maybe we should delete the extents here?  As this code doesn't
  914.     ;; remove them from widget structures, maybe it's safer to just
  915.     ;; detach them.  That's what `delete-overlay' did.
  916.     (when button
  917.       (detach-extent button))
  918.     (when sample
  919.       (detach-extent sample))
  920.     (when doc
  921.       (detach-extent doc))
  922.     (when field
  923.       (detach-extent field))
  924.     (mapc 'widget-leave-text children)))
  925.  
  926.  
  927. ;;; Keymap and Commands.
  928.  
  929. (defvar widget-keymap nil
  930.   "Keymap containing useful binding for buffers containing widgets.
  931. Recommended as a parent keymap for modes using widgets.")
  932.  
  933. (unless widget-keymap
  934.   (setq widget-keymap (make-sparse-keymap))
  935.   (define-key widget-keymap [tab] 'widget-forward)
  936.   (define-key widget-keymap [(shift tab)] 'widget-backward)
  937.   (define-key widget-keymap [(meta tab)] 'widget-backward)
  938.   (define-key widget-keymap [backtab] 'widget-backward))
  939.  
  940. (defvar widget-global-map global-map
  941.   "Keymap used for events the widget does not handle themselves.")
  942. (make-variable-buffer-local 'widget-global-map)
  943.  
  944. (defvar widget-field-keymap nil
  945.   "Keymap used inside an editable field.")
  946.  
  947. (unless widget-field-keymap
  948.   (setq widget-field-keymap (make-sparse-keymap))
  949.   (set-keymap-parents widget-field-keymap global-map)
  950.   (define-key widget-field-keymap "\C-k" 'widget-kill-line)
  951.   (define-key widget-field-keymap [(meta tab)] 'widget-complete)
  952.   (define-key widget-field-keymap [tab] 'widget-forward)
  953.   (define-key widget-field-keymap [(shift tab)] 'widget-backward)
  954.   (define-key widget-field-keymap "\C-m" 'widget-field-activate)
  955.   (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
  956.   (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
  957.   (define-key widget-field-keymap "\C-t" 'widget-transpose-chars))
  958.  
  959. (defvar widget-text-keymap nil
  960.   "Keymap used inside a text field.")
  961.  
  962. (unless widget-text-keymap
  963.   (setq widget-text-keymap (make-sparse-keymap))
  964.   (set-keymap-parents widget-field-keymap global-map)
  965.   (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
  966.   (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
  967.   (define-key widget-text-keymap "\C-t" 'widget-transpose-chars))
  968.  
  969. (defvar widget-button-keymap nil
  970.   "Keymap used inside a button.")
  971.  
  972. (unless widget-button-keymap
  973.   (setq widget-button-keymap (make-sparse-keymap))
  974.   (set-keymap-parents widget-button-keymap widget-keymap)
  975.   (define-key widget-button-keymap "\C-m" 'widget-button-press)
  976.   (define-key widget-button-keymap [button2] 'widget-button-click)
  977.   ;; Ideally, button3 within a button should invoke a button-specific
  978.   ;; menu.
  979.   (define-key widget-button-keymap [button3] 'widget-button-click)
  980.   ;;Glyph support.
  981.   (define-key widget-button-keymap [button1] 'widget-button1-click))
  982.  
  983.  
  984. (defun widget-field-activate (pos &optional event)
  985.   "Invoke the ediable field at point."
  986.   (interactive "@d")
  987.   (let ((field (widget-field-find pos)))
  988.     (if field
  989.     (widget-apply-action field event)
  990.       (call-interactively
  991.        (lookup-key widget-global-map (this-command-keys))))))
  992.  
  993. (defface widget-button-pressed-face
  994.   '((((class color))
  995.      (:foreground "red"))
  996.     (t
  997.      (:bold t :underline t)))
  998.   "Face used for pressed buttons."
  999.   :group 'widget-faces)
  1000.  
  1001. (defun widget-event-point (event)
  1002.   "Character position of the mouse event, or nil."
  1003.   (and (mouse-event-p event)
  1004.        (event-point event)))
  1005.  
  1006. (defun widget-button-click (event)
  1007.   "Invoke button below mouse pointer."
  1008.   (interactive "@e")
  1009.   (cond ((event-glyph event)
  1010.      (widget-glyph-click event))
  1011.     ((widget-event-point event)
  1012.      (let* ((pos (widget-event-point event))
  1013.         (button (get-char-property pos 'button)))
  1014.        (if button
  1015.            (let* ((extent (widget-get button :button-extent))
  1016.               (face (extent-property extent 'face))
  1017.               (mouse-face (extent-property extent 'mouse-face))
  1018.               (help-echo (extent-property extent 'help-echo)))
  1019.          (unwind-protect
  1020.              (progn
  1021.                ;; Merge relevant faces, and make the result mouse-face.
  1022.                (let ((merge `(widget-button-pressed-face ,mouse-face)))
  1023.              (nconc merge (if (listp face)
  1024.                       face (list face)))
  1025.              (setq merge (delete-if-not 'find-face merge))
  1026.              (set-extent-property extent 'mouse-face merge))
  1027.                (unless (widget-apply button :mouse-down-action event)
  1028.              ;; Wait for button release.
  1029.              (while (not (button-release-event-p
  1030.                       (setq event (next-event))))
  1031.                (dispatch-event event)))
  1032.                ;; Disallow mouse-face and help-echo.
  1033.                (set-extent-property extent 'mouse-face nil)
  1034.                (set-extent-property extent 'help-echo nil)
  1035.                (setq pos (widget-event-point event))
  1036.                (unless (eq (current-buffer) (extent-object extent))
  1037.              ;; Barf if dispatch-event tripped us by
  1038.              ;; changing buffer.
  1039.              (error "Buffer changed during mouse motion"))
  1040.                ;; Do the associated action.
  1041.                (when (and pos (extent-in-region-p extent pos pos))
  1042.              (widget-apply-action button event)))
  1043.            ;; Unwinding: fully release the button.
  1044.            (set-extent-property extent 'mouse-face mouse-face)
  1045.            (set-extent-property extent 'help-echo help-echo)))
  1046.          ;; This should not happen!
  1047.          (error "`widget-button-click' called outside button"))))
  1048.     (t
  1049.      (message "You clicked somewhere weird"))))
  1050.  
  1051. (defun widget-button1-click (event)
  1052.   "Invoke glyph below mouse pointer."
  1053.   (interactive "@e")
  1054.   (if (event-glyph event)
  1055.       (widget-glyph-click event)
  1056.     ;; Should somehow avoid this.
  1057.     (let ((command (lookup-key widget-global-map (this-command-keys))))
  1058.       (and (commandp command)
  1059.        (call-interactively command)))))
  1060.  
  1061. (defun widget-glyph-click (event)
  1062.   "Handle click on a glyph."
  1063.   (let* ((glyph (event-glyph event))
  1064.      (extent (event-glyph-extent event))
  1065.      (widget (extent-property extent 'glyph-widget))
  1066.      (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
  1067.      (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
  1068.      (last event))
  1069.     (unless (widget-apply widget :active)
  1070.       (error "This widget is inactive"))
  1071.     (let ((current-glyph 'down))
  1072.       ;; We always know what glyph is drawn currently, to avoid
  1073.       ;; unnecessary extent changes.  Is this any noticable gain?
  1074.       (unwind-protect
  1075.       (progn
  1076.         ;; Press the glyph.
  1077.         (set-extent-end-glyph extent down-glyph)
  1078.         ;; Redisplay (shouldn't be needed, but...)
  1079.         (sit-for 0)
  1080.         (unless (widget-apply widget :mouse-down-action event)
  1081.           ;; Wait for the release.
  1082.           (while (not (button-release-event-p last))
  1083.         (unless (button-press-event-p last)
  1084.           (dispatch-event last))
  1085.         (when (motion-event-p last)
  1086.           ;; Update glyphs on mouse motion.
  1087.           (if (eq extent (event-glyph-extent last))
  1088.               (unless (eq current-glyph 'down)
  1089.             (set-extent-end-glyph extent down-glyph)
  1090.             (setq current-glyph 'down))
  1091.             (unless (eq current-glyph 'up)
  1092.               (set-extent-end-glyph extent up-glyph)
  1093.               (setq current-glyph 'up))))
  1094.         (setq last (next-event event))))
  1095.         (unless (eq (current-buffer) (extent-object extent))
  1096.           ;; Barf if dispatch-event tripped us by changing buffer.
  1097.           (error "Buffer changed during mouse motion"))
  1098.         ;; Apply widget action.
  1099.         (when (eq extent (event-glyph-extent last))
  1100.           (let ((widget (extent-property (event-glyph-extent event)
  1101.                          'glyph-widget)))
  1102.         (cond ((null widget)
  1103.                (message "You clicked on a glyph"))
  1104.               ((not (widget-apply widget :active))
  1105.                (error "This glyph is inactive"))
  1106.               (t
  1107.                (widget-apply-action widget event))))))
  1108.     ;; Release the glyph.
  1109.     (and (eq current-glyph 'down)
  1110.          ;; The extent might have been detached or deleted
  1111.          (extent-live-p extent)
  1112.          (not (extent-detached-p extent))
  1113.          (set-extent-end-glyph extent up-glyph))))))
  1114.  
  1115. (defun widget-button-press (pos &optional event)
  1116.   "Invoke button at POS."
  1117.   (interactive "@d")
  1118.   (let ((button (get-char-property pos 'button)))
  1119.     (if button
  1120.     (widget-apply-action button event)
  1121.       (let ((command (lookup-key widget-global-map (this-command-keys))))
  1122.     (when (commandp command)
  1123.       (call-interactively command))))))
  1124.  
  1125. (defun widget-tabable-at (&optional pos last-tab backwardp)
  1126.   "Return the tabable widget at POS, or nil.
  1127. POS defaults to the value of (point)."
  1128.   (unless pos
  1129.     (setq pos (point)))
  1130.   (let ((widget (widget-at pos)))
  1131.     (if widget
  1132.     (let ((order (widget-get widget :tab-order)))
  1133.       (if order
  1134.           (if last-tab (and (= order (if backwardp
  1135.                          (1- last-tab)
  1136.                        (1+ last-tab)))
  1137.                 widget)
  1138.         (and (> order 0) widget))
  1139.         widget))
  1140.       nil)))
  1141.  
  1142. ;; Return the button or field extent at point.
  1143. (defun widget-button-or-field-extent (pos)
  1144.   (or (and (get-char-property pos 'button)
  1145.        (widget-get (get-char-property pos 'button)
  1146.                :button-extent))
  1147.       (and (get-char-property pos 'field)
  1148.        (widget-get (get-char-property pos 'field)
  1149.                :field-extent))))
  1150.  
  1151. (defun widget-next-button-or-field (pos)
  1152.   "Find the next button, or field, and return its start position, or nil.
  1153. Internal function, don't use it outside `wid-edit'."
  1154.   (let* ((at-point (widget-button-or-field-extent pos))
  1155.      (extent (map-extents
  1156.           (lambda (ext ignore)
  1157.             ext)
  1158.           nil (if at-point (extent-end-position at-point) pos)
  1159.           nil nil 'start-open 'button-or-field)))
  1160.     (and extent
  1161.      (extent-start-position extent))))
  1162.  
  1163. ;; This is too slow in buffers with many buttons (W3).
  1164. (defun widget-previous-button-or-field (pos)
  1165.   "Find the previous button, or field, and return its start position, or nil.
  1166. Internal function, don't use it outside `wid-edit'."
  1167.   (let* ((at-point (widget-button-or-field-extent pos))
  1168.      previous-extent)
  1169.     (map-extents
  1170.      (lambda (ext ignore)
  1171.        (if (eq ext at-point)
  1172.        ;; We reached the extent we were on originally
  1173.        (if (= pos (extent-start-position at-point))
  1174.            previous-extent
  1175.          (setq previous-extent at-point))
  1176.      (setq previous-extent ext)
  1177.      nil))
  1178.      nil nil pos nil 'start-open 'button-or-field)
  1179.     (and previous-extent
  1180.      (extent-start-position previous-extent))))
  1181.  
  1182. (defun widget-move (arg)
  1183.   "Move point to the ARG next field or button.
  1184. ARG may be negative to move backward."
  1185.   (let ((opoint (point)) (wrapped 0)
  1186.     (last-tab (widget-get (widget-at (point)) :tab-order))
  1187.     nextpos found)
  1188.     ;; Movement backward
  1189.     (while (< arg 0)
  1190.       (setq nextpos (widget-previous-button-or-field (point)))
  1191.       (if nextpos
  1192.       (progn
  1193.         (goto-char nextpos)
  1194.         (when (and (not (get-char-property nextpos 'widget-inactive))
  1195.                (widget-tabable-at nil last-tab t))
  1196.           (incf arg)
  1197.           (setq found t
  1198.             last-tab (widget-get (widget-at (point))
  1199.                      :tab-order))))
  1200.     (if (and (not found) (> wrapped 1))
  1201.         (setq arg 0
  1202.           found nil)
  1203.       (goto-char (point-max))
  1204.       (incf wrapped))))
  1205.     ;; Movement forward
  1206.     (while (> arg 0)
  1207.       (setq nextpos (widget-next-button-or-field (point)))
  1208.       (if nextpos
  1209.       (progn
  1210.         (goto-char nextpos)
  1211.         (when (and (not (get-char-property nextpos 'widget-inactive))
  1212.                (widget-tabable-at nil last-tab))
  1213.           (decf arg)
  1214.           (setq found t
  1215.             last-tab (widget-get (widget-at (point))
  1216.                      :tab-order))))
  1217.     (if (and (not found) (> wrapped 1))
  1218.         (setq arg 0
  1219.           found nil)
  1220.       (goto-char (point-min))
  1221.       (incf wrapped))))
  1222.     (if (not found)
  1223.     (goto-char opoint)
  1224.       (widget-echo-help (point))
  1225.       (run-hooks 'widget-move-hook))))
  1226.  
  1227. (defun widget-forward (arg)
  1228.   "Move point to the next field or button.
  1229. With optional ARG, move across that many fields."
  1230.   (interactive "p")
  1231.   (run-hooks 'widget-forward-hook)
  1232.   (widget-move arg))
  1233.  
  1234. (defun widget-backward (arg)
  1235.   "Move point to the previous field or button.
  1236. With optional ARG, move across that many fields."
  1237.   (interactive "p")
  1238.   (run-hooks 'widget-backward-hook)
  1239.   (widget-move (- arg)))
  1240.  
  1241. (defun widget-beginning-of-line ()
  1242.   "Go to beginning of field or beginning of line, whichever is first."
  1243.   (interactive "_")
  1244.   (let* ((field (widget-field-find (point)))
  1245.      (start (and field (widget-field-start field))))
  1246.     (if (and start (not (eq start (point))))
  1247.     (goto-char start)
  1248.       (call-interactively 'beginning-of-line))))
  1249.  
  1250. (defun widget-end-of-line ()
  1251.   "Go to end of field or end of line, whichever is first."
  1252.   (interactive "_")
  1253.   (let* ((field (widget-field-find (point)))
  1254.      (end (and field (widget-field-end field))))
  1255.     (if (and end (not (eq end (point))))
  1256.     (goto-char end)
  1257.       (call-interactively 'end-of-line))))
  1258.  
  1259. (defun widget-kill-line ()
  1260.   "Kill to end of field or end of line, whichever is first."
  1261.   (interactive)
  1262.   (let* ((field (widget-field-find (point)))
  1263.      (newline (save-excursion (forward-line 1) (point)))
  1264.      (end (and field (widget-field-end field))))
  1265.     (if (and field (> newline end))
  1266.     (kill-region (point) end)
  1267.       (call-interactively 'kill-line))))
  1268.  
  1269. (defun widget-transpose-chars (arg)
  1270.   "Like `transpose-chars', but works correctly at end of widget."
  1271.   (interactive "*P")
  1272.   (let* ((field (widget-field-find (point)))
  1273.      (start (and field (widget-field-start field)))
  1274.      (end (and field (widget-field-end field)))
  1275.      (last-non-space (and start end
  1276.                   (save-excursion
  1277.                 (goto-char end)
  1278.                 (skip-chars-backward " \t\n" start)
  1279.                 (point)))))
  1280.     (cond ((and last-non-space
  1281.         (or (= last-non-space start)
  1282.             (= last-non-space (1+ start))))
  1283.        ;; empty or one-character field
  1284.        nil)
  1285.       ((= (point) start)
  1286.        ;; at the beginning of the field -- we would get an error here.
  1287.        (error "Cannot transpose at beginning of field"))
  1288.       (t
  1289.        (when (and (null arg)
  1290.               (= last-non-space (point)))
  1291.          (forward-char -1))
  1292.        (transpose-chars arg)))))
  1293.  
  1294. (defcustom widget-complete-field (lookup-key global-map "\M-\t")
  1295.   "Default function to call for completion inside fields."
  1296.   :options '(ispell-complete-word complete-tag lisp-complete-symbol)
  1297.   :type 'function
  1298.   :group 'widgets)
  1299.  
  1300. (defun widget-complete ()
  1301.   "Complete content of editable field from point.
  1302. When not inside a field, move to the previous button or field."
  1303.   (interactive)
  1304.   ;; Somehow, this should make pressing M-TAB twice scroll the
  1305.   ;; completions window.
  1306.   (let ((field (widget-field-find (point))))
  1307.     (if field
  1308.     (widget-apply field :complete)
  1309.       (error "Not in an editable field"))))
  1310.  
  1311.  
  1312. ;;; Setting up the buffer.
  1313.  
  1314. (defvar widget-field-new nil)
  1315. ;; List of all newly created editable fields in the buffer.
  1316. (make-variable-buffer-local 'widget-field-new)
  1317.  
  1318. (defvar widget-field-list nil)
  1319. ;; List of all editable fields in the buffer.
  1320. (make-variable-buffer-local 'widget-field-list)
  1321.  
  1322. (defun widget-setup ()
  1323.   "Setup current buffer so editing string widgets works."
  1324.   (let ((inhibit-read-only t)
  1325.     (after-change-functions nil)
  1326.     before-change-functions
  1327.     field)
  1328.     (while widget-field-new
  1329.       (setq field (car widget-field-new)
  1330.         widget-field-new (cdr widget-field-new)
  1331.         widget-field-list (cons field widget-field-list))
  1332.       (let ((from (car (widget-get field :field-extent)))
  1333.         (to (cdr (widget-get field :field-extent))))
  1334.     (widget-specify-field field
  1335.                   (marker-position from) (marker-position to))
  1336.     (set-marker from nil)
  1337.     (set-marker to nil))
  1338.       ;; If the field is placed within the inactive zone, deactivate it.
  1339.       (let ((extent (widget-get field :field-extent)))
  1340.     (when (get-char-property (extent-start-position extent)
  1341.                  'widget-inactive)
  1342.       (widget-activation-widget-mapper extent :deactivate)))))
  1343.   (widget-clear-undo)
  1344.   (widget-add-change))
  1345.  
  1346. (defvar widget-field-last nil)
  1347. ;; Last field containing point.
  1348. (make-variable-buffer-local 'widget-field-last)
  1349.  
  1350. (defvar widget-field-was nil)
  1351. ;; The widget data before the change.
  1352. (make-variable-buffer-local 'widget-field-was)
  1353.  
  1354. (defun widget-field-buffer (widget)
  1355.   "Return the start of WIDGET's editing field."
  1356.   (let ((extent (widget-get widget :field-extent)))
  1357.     (and extent (extent-object extent))))
  1358.  
  1359. (defun widget-field-start (widget)
  1360.   "Return the start of WIDGET's editing field."
  1361.   (let ((extent (widget-get widget :field-extent)))
  1362.     (and extent (extent-start-position extent))))
  1363.  
  1364. (defun widget-field-end (widget)
  1365.   "Return the end of WIDGET's editing field."
  1366.   (let ((extent (widget-get widget :field-extent)))
  1367.     ;; Don't subtract one if local-map works at the end of the extent.
  1368.     (and extent (if (or widget-field-add-space
  1369.             (null (widget-get widget :size)))
  1370.             (1- (extent-end-position extent))
  1371.           (extent-end-position extent)))))
  1372.  
  1373. (defun widget-field-find (pos)
  1374.   "Return the field at POS.
  1375. Unlike (get-char-property POS 'field) this, works with empty fields too."
  1376.   (let ((field-extent (map-extents (lambda (extent ignore)
  1377.                      extent)
  1378.                    nil pos pos nil nil 'field)))
  1379.     (and field-extent
  1380.      (extent-property field-extent 'field))))
  1381.  
  1382. ;; Old version, without `map-extents'.
  1383. ;(defun widget-field-find (pos)
  1384. ;  (let ((fields widget-field-list)
  1385. ;    field found)
  1386. ;    (while fields
  1387. ;      (setq field (car fields)
  1388. ;        fields (cdr fields))
  1389. ;      (let ((start (widget-field-start field))
  1390. ;        (end (widget-field-end field)))
  1391. ;    (when (and (<= start pos) (<= pos end))
  1392. ;      (when found
  1393. ;        (debug "Overlapping fields"))
  1394. ;      (setq found field))))
  1395. ;    found))
  1396.  
  1397. (defun widget-before-change (from to)
  1398.   ;; Barf if the text changed is outside the editable fields.
  1399.   (unless inhibit-read-only
  1400.     (let ((from-field (widget-field-find from))
  1401.       (to-field (widget-field-find to)))
  1402.       (cond ((or (null from-field)
  1403.          (null to-field))
  1404.          ;; Either end of change is not within a field.
  1405.          (add-hook 'post-command-hook 'widget-add-change nil t)
  1406.          (error "Attempt to change text outside editable field"))
  1407.         ((not (eq from-field to-field))
  1408.          ;; The change begins in one fields, and ends in another one.
  1409.          (add-hook 'post-command-hook 'widget-add-change nil t)
  1410.          (error "Change should be restricted to a single field"))
  1411.         ((or (and from-field
  1412.               (get-char-property from 'widget-inactive))
  1413.          (and to-field
  1414.               (get-char-property to 'widget-inactive)))
  1415.          ;; Trying to change an inactive editable field.
  1416.          (add-hook 'post-command-hook 'widget-add-change nil t)
  1417.          (error "Attempt to change an inactive field"))
  1418.         (widget-field-use-before-change
  1419.          ;; #### Bletch!  This loses because XEmacs get confused
  1420.          ;; if before-change-functions change the contents of
  1421.          ;; buffer before from/to.
  1422.          (condition-case nil
  1423.          (widget-apply from-field :notify from-field)
  1424.            (error (debug "Before Change"))))))))
  1425.  
  1426. (defun widget-add-change ()
  1427.   (make-local-hook 'post-command-hook)
  1428.   (remove-hook 'post-command-hook 'widget-add-change t)
  1429.   (make-local-hook 'before-change-functions)
  1430.   (add-hook 'before-change-functions 'widget-before-change nil t)
  1431.   (make-local-hook 'after-change-functions)
  1432.   (add-hook 'after-change-functions 'widget-after-change nil t))
  1433.  
  1434. (defun widget-after-change (from to old)
  1435.   ;; Adjust field size and text properties.
  1436.  
  1437.   ;; Also, notify the widgets (so, for example, a variable changes its
  1438.   ;; state to `modified'.  when it is being edited.)
  1439.   (condition-case nil
  1440.       (let ((field (widget-field-find from))
  1441.         (other (widget-field-find to)))
  1442.     (when field
  1443.       (unless (eq field other)
  1444.         (debug "Change in different fields"))
  1445.       (let ((size (widget-get field :size))
  1446.         (secret (widget-get field :secret)))
  1447.         (when size
  1448.           (let ((begin (widget-field-start field))
  1449.             (end (widget-field-end field)))
  1450.         (cond ((< (- end begin) size)
  1451.                ;; Field too small.
  1452.                (save-excursion
  1453.              (goto-char end)
  1454.              (insert-char ?\  (- (+ begin size) end))))
  1455.               ((> (- end begin) size)
  1456.                ;; Field too large and
  1457.                (if (or (< (point) (+ begin size))
  1458.                    (> (point) end))
  1459.                ;; Point is outside extra space.
  1460.                (setq begin (+ begin size))
  1461.              ;; Point is within the extra space.
  1462.              (setq begin (point)))
  1463.                (save-excursion
  1464.              (goto-char end)
  1465.              (while (and (eq (preceding-char) ?\ )
  1466.                      (> (point) begin))
  1467.                (delete-backward-char 1)))))))
  1468.         (when secret
  1469.           (let ((begin (widget-field-start field))
  1470.             (end (widget-field-end field)))
  1471.         (when size
  1472.           (while (and (> end begin)
  1473.                   (eq (char-after (1- end)) ?\ ))
  1474.             (setq end (1- end))))
  1475.         (while (< begin end)
  1476.           (let ((old (char-after begin)))
  1477.             (unless (eq old secret)
  1478.               (subst-char-in-region begin (1+ begin) old secret)
  1479.               (put-text-property begin (1+ begin) 'secret old))
  1480.             (incf begin))))))
  1481.       (widget-apply field :notify field)))
  1482.     (error (debug "After Change"))))
  1483.  
  1484.  
  1485. ;;; Widget Functions
  1486. ;;
  1487. ;; These functions are used in the definition of multiple widgets.
  1488.  
  1489. (defun widget-parent-action (widget &optional event)
  1490.   "Tell :parent of WIDGET to handle the :action.
  1491. Optional EVENT is the event that triggered the action."
  1492.   (widget-apply (widget-get widget :parent) :action event))
  1493.  
  1494. (defun widget-children-value-delete (widget)
  1495.   "Delete all :children and :buttons in WIDGET."
  1496.   (mapc 'widget-delete (widget-get widget :children))
  1497.   (widget-put widget :children nil)
  1498.   (mapc 'widget-delete (widget-get widget :buttons))
  1499.   (widget-put widget :buttons nil))
  1500.  
  1501. (defun widget-children-validate (widget)
  1502.   "All the :children must be valid."
  1503.   (let ((children (widget-get widget :children))
  1504.     child found)
  1505.     (while (and children (not found))
  1506.       (setq child (car children)
  1507.         children (cdr children)
  1508.         found (widget-apply child :validate)))
  1509.     found))
  1510.  
  1511. (defun widget-types-convert-widget (widget)
  1512.   "Convert :args as widget types in WIDGET."
  1513.   (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
  1514.   widget)
  1515.  
  1516. (defun widget-value-convert-widget (widget)
  1517.   "Initialize :value from :args in WIDGET."
  1518.   (let ((args (widget-get widget :args)))
  1519.     (when args
  1520.       (widget-put widget :value (car args))
  1521.       ;; Don't convert :value here, as this is done in `widget-convert'.
  1522.       ;; (widget-put widget :value (widget-apply widget
  1523.       ;; :value-to-internal (car args)))
  1524.       (widget-put widget :args nil)))
  1525.   widget)
  1526.  
  1527. (defun widget-value-value-get (widget)
  1528.   "Return the :value property of WIDGET."
  1529.   (widget-get widget :value))
  1530.  
  1531. ;;; The `default' Widget.
  1532.  
  1533. (define-widget 'default nil
  1534.   "Basic widget other widgets are derived from."
  1535.   :value-to-internal (lambda (widget value) value)
  1536.   :value-to-external (lambda (widget value) value)
  1537.   :button-prefix 'widget-button-prefix
  1538.   :button-suffix 'widget-button-suffix
  1539.   :complete 'widget-default-complete
  1540.   :create 'widget-default-create
  1541.   :indent nil
  1542.   :offset 0
  1543.   :format-handler 'widget-default-format-handler
  1544.   :button-face-get 'widget-default-button-face-get
  1545.   :sample-face-get 'widget-default-sample-face-get
  1546.   :button-keymap widget-button-keymap
  1547.   :delete 'widget-default-delete
  1548.   :value-set 'widget-default-value-set
  1549.   :value-inline 'widget-default-value-inline
  1550.   :menu-tag-get 'widget-default-menu-tag-get
  1551.   :validate (lambda (widget) nil)
  1552.   :active 'widget-default-active
  1553.   :activate 'widget-specify-active
  1554.   :deactivate 'widget-default-deactivate
  1555.   :mouse-down-action (lambda (widget event) nil)
  1556.   :action 'widget-default-action
  1557.   :notify 'widget-default-notify
  1558.   :prompt-value 'widget-default-prompt-value)
  1559.  
  1560. (defun widget-default-complete (widget)
  1561.   "Call the value of the :complete-function property of WIDGET.
  1562. If that does not exists, call the value of `widget-complete-field'."
  1563.   (let ((fun (widget-get widget :complete-function)))
  1564.     (call-interactively (or fun widget-complete-field))))
  1565.  
  1566. (defun widget-default-create (widget)
  1567.   "Create WIDGET at point in the current buffer."
  1568.   (widget-specify-insert
  1569.    (let ((from (point))
  1570.      button-begin button-end button-glyph
  1571.      sample-begin sample-end
  1572.      doc-begin doc-end
  1573.      value-pos)
  1574.      (insert (widget-get widget :format))
  1575.      (goto-char from)
  1576.      ;; Parse escapes in format.  Coding this in C would speed up
  1577.      ;; things *a lot*.
  1578.      (while (re-search-forward "%\\(.\\)" nil t)
  1579.        (let ((escape (aref (match-string 1) 0)))
  1580.      (replace-match "" t t)
  1581.      (cond ((eq escape ?%)
  1582.         (insert "%"))
  1583.            ((eq escape ?\[)
  1584.         (setq button-begin (point-marker))
  1585.         (set-marker-insertion-type button-begin nil))
  1586.            ((eq escape ?\])
  1587.         (setq button-end (point-marker))
  1588.         (set-marker-insertion-type button-end nil))
  1589.            ((eq escape ?\{)
  1590.         (setq sample-begin (point)))
  1591.            ((eq escape ?\})
  1592.         (setq sample-end (point)))
  1593.            ((eq escape ?n)
  1594.         (when (widget-get widget :indent)
  1595.           (insert "\n")
  1596.           (insert-char ?\  (widget-get widget :indent))))
  1597.            ((eq escape ?t)
  1598.         (let* ((tag (widget-get widget :tag))
  1599.                (glyph (widget-get widget :tag-glyph)))
  1600.           (cond (glyph
  1601.              (setq button-glyph
  1602.                    (widget-glyph-insert
  1603.                 widget (or tag "Image") glyph)))
  1604.             (tag
  1605.              (insert tag))
  1606.             (t
  1607.              (let ((standard-output (current-buffer)))
  1608.                (princ (widget-get widget :value)))))))
  1609.            ((eq escape ?d)
  1610.         (let ((doc (widget-get widget :doc)))
  1611.           (when doc
  1612.             (setq doc-begin (point))
  1613.             (insert doc)
  1614.             (while (eq (preceding-char) ?\n)
  1615.               (delete-backward-char 1))
  1616.             (insert "\n")
  1617.             (setq doc-end (point)))))
  1618.            ((eq escape ?v)
  1619.         (if (and button-begin (not button-end))
  1620.             (widget-apply widget :value-create)
  1621.           (setq value-pos (point-marker))))
  1622.            (t
  1623.         (widget-apply widget :format-handler escape)))))
  1624.      ;; Specify button, sample, and doc, and insert value.
  1625.      (when (and button-begin button-end)
  1626.        (unless button-glyph
  1627.      (goto-char button-begin)
  1628.      (insert (widget-get-indirect widget :button-prefix))
  1629.      (goto-char button-end)
  1630.      (set-marker-insertion-type button-end t)
  1631.      (insert (widget-get-indirect widget :button-suffix)))
  1632.        (widget-specify-button widget button-begin button-end)
  1633.        ;; Is this necessary?
  1634.        (set-marker button-begin nil)
  1635.        (set-marker button-end nil))
  1636.      (and sample-begin sample-end
  1637.       (widget-specify-sample widget sample-begin sample-end))
  1638.      (and doc-begin doc-end
  1639.       (widget-specify-doc widget doc-begin doc-end))
  1640.      (when value-pos
  1641.        (goto-char value-pos)
  1642.        (widget-apply widget :value-create)))
  1643.    (let ((from (point-min-marker))
  1644.      (to (point-max-marker)))
  1645.      (set-marker-insertion-type from t)
  1646.      (set-marker-insertion-type to nil)
  1647.      (widget-put widget :from from)
  1648.      (widget-put widget :to to)))
  1649.   (widget-clear-undo))
  1650.  
  1651. (defun widget-default-format-handler (widget escape)
  1652.   ;; We recognize the %h escape by default.
  1653.   (let* ((buttons (widget-get widget :buttons)))
  1654.     (cond ((eq escape ?h)
  1655.        (let* ((doc-property (widget-get widget :documentation-property))
  1656.           (doc-try (cond ((widget-get widget :doc))
  1657.                  ((symbolp doc-property)
  1658.                   (documentation-property
  1659.                    (widget-get widget :value)
  1660.                    doc-property))
  1661.                  (t
  1662.                   (funcall doc-property
  1663.                        (widget-get widget :value)))))
  1664.           (doc-text (and (stringp doc-try)
  1665.                  (> (length doc-try) 1)
  1666.                  doc-try))
  1667.           (doc-indent (widget-get widget :documentation-indent)))
  1668.          (when doc-text
  1669.            (and (eq (preceding-char) ?\n)
  1670.             (widget-get widget :indent)
  1671.             (insert-char ?\  (widget-get widget :indent)))
  1672.            ;; The `*' in the beginning is redundant.
  1673.            (when (eq (aref doc-text  0) ?*)
  1674.          (setq doc-text (substring doc-text 1)))
  1675.            ;; Get rid of trailing newlines.
  1676.            (when (string-match "\n+\\'" doc-text)
  1677.          (setq doc-text (substring doc-text 0 (match-beginning 0))))
  1678.            (push (widget-create-child-and-convert
  1679.               widget 'documentation-string
  1680.               :indent (cond ((numberp doc-indent)
  1681.                      doc-indent)
  1682.                     ((null doc-indent)
  1683.                      nil)
  1684.                     (t 0))
  1685.               doc-text)
  1686.              buttons))))
  1687.       (t
  1688.        (error "Unknown escape `%c'" escape)))
  1689.     (widget-put widget :buttons buttons)))
  1690.  
  1691. (defun widget-default-button-face-get (widget)
  1692.   ;; Use :button-face or widget-button-face
  1693.   (or (widget-get widget :button-face)
  1694.       (let ((parent (widget-get widget :parent)))
  1695.     (if parent
  1696.         (widget-apply parent :button-face-get)
  1697.       widget-button-face))))
  1698.  
  1699. (defun widget-default-sample-face-get (widget)
  1700.   ;; Use :sample-face.
  1701.   (widget-get widget :sample-face))
  1702.  
  1703. (defun widget-default-delete (widget)
  1704.   ;; Remove widget from the buffer.
  1705.   (let ((from (widget-get widget :from))
  1706.     (to (widget-get widget :to))
  1707.     (inactive-extent (widget-get widget :inactive))
  1708.     (button-extent (widget-get widget :button-extent))
  1709.     (sample-extent (widget-get widget :sample-extent))
  1710.     (doc-extent (widget-get widget :doc-extent))
  1711.     before-change-functions
  1712.     after-change-functions
  1713.     (inhibit-read-only t))
  1714.     (widget-apply widget :value-delete)
  1715.     (when inactive-extent
  1716.       (detach-extent inactive-extent))
  1717.     (when button-extent
  1718.       (detach-extent button-extent))
  1719.     (when sample-extent
  1720.       (detach-extent sample-extent))
  1721.     (when doc-extent
  1722.       (detach-extent doc-extent))
  1723.     (when (< from to)
  1724.       ;; Kludge: this doesn't need to be true for empty formats.
  1725.       (delete-region from to))
  1726.     (set-marker from nil)
  1727.     (set-marker to nil))
  1728.   (widget-clear-undo))
  1729.  
  1730. (defun widget-default-value-set (widget value)
  1731.   ;; Recreate widget with new value.
  1732.   (let* ((old-pos (point))
  1733.      (from (copy-marker (widget-get widget :from)))
  1734.      (to (copy-marker (widget-get widget :to)))
  1735.      (offset (if (and (<= from old-pos) (<= old-pos to))
  1736.              (if (>= old-pos (1- to))
  1737.              (- old-pos to 1)
  1738.                (- old-pos from)))))
  1739.     ;;??? Bug: this ought to insert the new value before deleting the old one,
  1740.     ;; so that markers on either side of the value automatically
  1741.     ;; stay on the same side.  -- rms.
  1742.     (save-excursion
  1743.       (goto-char (widget-get widget :from))
  1744.       (widget-apply widget :delete)
  1745.       (widget-put widget :value value)
  1746.       (widget-apply widget :create))
  1747.     (when offset
  1748.       (if (< offset 0)
  1749.       (goto-char (+ (widget-get widget :to) offset 1))
  1750.     (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
  1751.  
  1752. (defun widget-default-value-inline (widget)
  1753.   ;; Wrap value in a list unless it is inline.
  1754.   (if (widget-get widget :inline)
  1755.       (widget-value widget)
  1756.     (list (widget-value widget))))
  1757.  
  1758. (defun widget-default-menu-tag-get (widget)
  1759.   ;; Use tag or value for menus.
  1760.   (or (widget-get widget :menu-tag)
  1761.       (widget-get widget :tag)
  1762.       (widget-princ-to-string (widget-get widget :value))))
  1763.  
  1764. (defun widget-default-active (widget)
  1765.   "Return t iff this widget active (user modifiable)."
  1766.   (and (not (widget-get widget :inactive))
  1767.        (let ((parent (widget-get widget :parent)))
  1768.      (or (null parent)
  1769.          (widget-apply parent :active)))))
  1770.  
  1771. (defun widget-default-deactivate (widget)
  1772.   "Make WIDGET inactive for user modifications."
  1773.   (widget-specify-inactive widget
  1774.                (widget-get widget :from)
  1775.                (widget-get widget :to)))
  1776.  
  1777. (defun widget-default-action (widget &optional event)
  1778.   ;; Notify the parent when a widget change
  1779.   (let ((parent (widget-get widget :parent)))
  1780.     (when parent
  1781.       (widget-apply parent :notify widget event))))
  1782.  
  1783. (defun widget-default-notify (widget child &optional event)
  1784.   ;; Pass notification to parent.
  1785.   (widget-default-action widget event))
  1786.  
  1787. (defun widget-default-prompt-value (widget prompt value unbound)
  1788.   ;; Read an arbitrary value.  Stolen from `set-variable'.
  1789. ;;  (let ((initial (if unbound
  1790. ;;             nil
  1791. ;;           ;; It would be nice if we could do a `(cons val 1)' here.
  1792. ;;           (prin1-to-string (custom-quote value))))))
  1793.   (eval-minibuffer prompt ))
  1794.  
  1795. ;;; The `item' Widget.
  1796.  
  1797. (define-widget 'item 'default
  1798.   "Constant items for inclusion in other widgets."
  1799.   :convert-widget 'widget-value-convert-widget
  1800.   :value-create 'widget-item-value-create
  1801.   :value-delete 'ignore
  1802.   :value-get 'widget-value-value-get
  1803.   :match 'widget-item-match
  1804.   :match-inline 'widget-item-match-inline
  1805.   :action 'widget-item-action
  1806.   :format "%t\n")
  1807.  
  1808. (defun widget-item-value-create (widget)
  1809.   ;; Insert the printed representation of the value.
  1810.   (let ((standard-output (current-buffer)))
  1811.     (princ (widget-get widget :value))))
  1812.  
  1813. (defun widget-item-match (widget value)
  1814.   ;; Match if the value is the same.
  1815.   (equal (widget-get widget :value) value))
  1816.  
  1817. (defun widget-item-match-inline (widget values)
  1818.   ;; Match if the value is the same.
  1819.   (let ((value (widget-get widget :value)))
  1820.     (and (listp value)
  1821.      (<= (length value) (length values))
  1822.      (let ((head (widget-sublist values 0 (length value))))
  1823.        (and (equal head value)
  1824.         (cons head (widget-sublist values (length value))))))))
  1825.  
  1826. (defun widget-sublist (list start &optional end)
  1827.   "Return the sublist of LIST from START to END.
  1828. If END is omitted, it defaults to the length of LIST."
  1829.   (if (> start 0) (setq list (nthcdr start list)))
  1830.   (if end
  1831.       (if (<= end start)
  1832.       nil
  1833.     (setq list (copy-sequence list))
  1834.     (setcdr (nthcdr (- end start 1) list) nil)
  1835.     list)
  1836.     (copy-sequence list)))
  1837.  
  1838. (defun widget-item-action (widget &optional event)
  1839.   ;; Just notify itself.
  1840.   (widget-apply widget :notify widget event))
  1841.  
  1842. ;;; The `push-button' Widget.
  1843.  
  1844. (defcustom widget-push-button-gui widget-glyph-enable
  1845.   "If non nil, use GUI push buttons when available."
  1846.   :group 'widgets
  1847.   :type 'boolean)
  1848.  
  1849. ;; Cache already created GUI objects.
  1850. (defvar widget-push-button-cache nil)
  1851.  
  1852. (defcustom widget-push-button-prefix "["
  1853.   "String used as prefix for buttons."
  1854.   :type 'string
  1855.   :group 'widget-button)
  1856.  
  1857. (defcustom widget-push-button-suffix "]"
  1858.   "String used as suffix for buttons."
  1859.   :type 'string
  1860.   :group 'widget-button)
  1861.  
  1862. (define-widget 'push-button 'item
  1863.   "A pushable button."
  1864.   :button-prefix ""
  1865.   :button-suffix ""
  1866.   :value-create 'widget-push-button-value-create
  1867.   :format "%[%v%]")
  1868.  
  1869. (defun widget-push-button-value-create (widget)
  1870.   ;; Insert text representing the `on' and `off' states.
  1871.   (let* ((tag (or (widget-get widget :tag)
  1872.           (widget-get widget :value)))
  1873.      (tag-glyph (widget-get widget :tag-glyph))
  1874.      (text (concat widget-push-button-prefix
  1875.                tag widget-push-button-suffix))
  1876.      (gui-glyphs (lax-plist-get widget-push-button-cache tag)))
  1877.     (cond (tag-glyph
  1878.        (widget-glyph-insert widget text tag-glyph))
  1879.       ;; We must check for console-on-window-system-p here,
  1880.       ;; because GUI will not work otherwise (it needs RGB
  1881.       ;; components for colors, and they are not known on TTYs).
  1882.       ((and widget-push-button-gui
  1883.         (console-on-window-system-p))
  1884.        (unless gui-glyphs
  1885.          (let* ((gui-button-shadow-thickness 1)
  1886.             (gui (make-gui-button tag 'widget-gui-action widget)))
  1887.            (setq
  1888.         gui-glyphs
  1889.         (list
  1890.          (make-glyph `(,(nth 0 (aref gui 1)) [string :data ,text]))
  1891.          (make-glyph `(,(nth 1 (aref gui 1)) [string :data ,text]))
  1892.          (make-glyph `(,(nth 2 (aref gui 1)) [string :data ,text]))))
  1893.            (laxputf widget-push-button-cache tag gui-glyphs)))
  1894.        (widget-glyph-insert-glyph
  1895.         widget (nth 0 gui-glyphs) (nth 1 gui-glyphs) (nth 2 gui-glyphs)))
  1896.       (t
  1897.        (insert text)))))
  1898.  
  1899. (defun widget-gui-action (widget)
  1900.   "Apply :action for WIDGET."
  1901.   (widget-apply-action widget (this-command-keys)))
  1902.  
  1903. ;;; The `link' Widget.
  1904.  
  1905. (defcustom widget-link-prefix "["
  1906.   "String used as prefix for links."
  1907.   :type 'string
  1908.   :group 'widget-button)
  1909.  
  1910. (defcustom widget-link-suffix "]"
  1911.   "String used as suffix for links."
  1912.   :type 'string
  1913.   :group 'widget-button)
  1914.  
  1915. (define-widget 'link 'item
  1916.   "An embedded link."
  1917.   :button-prefix 'widget-link-prefix
  1918.   :button-suffix 'widget-link-suffix
  1919.   :help-echo "Follow the link"
  1920.   :format "%[%t%]")
  1921.  
  1922. ;;; The `info-link' Widget.
  1923.  
  1924. (define-widget 'info-link 'link
  1925.   "A link to an info file."
  1926.   :help-echo 'widget-info-link-help-echo
  1927.   :action 'widget-info-link-action)
  1928.  
  1929. (defun widget-info-link-help-echo (widget)
  1930.   (concat "Read the manual entry `" (widget-value widget) "'"))
  1931.  
  1932. (defun widget-info-link-action (widget &optional event)
  1933.   "Open the info node specified by WIDGET."
  1934.   (Info-goto-node (widget-value widget)))
  1935.  
  1936. ;;; The `url-link' Widget.
  1937.  
  1938. (define-widget 'url-link 'link
  1939.   "A link to an www page."
  1940.   :help-echo 'widget-url-link-help-echo
  1941.   :action 'widget-url-link-action)
  1942.  
  1943. (defun widget-url-link-help-echo (widget)
  1944.   (concat "Visit <URL:" (widget-value widget) ">"))
  1945.  
  1946. (defun widget-url-link-action (widget &optional event)
  1947.   "Open the url specified by WIDGET."
  1948.   (require 'browse-url)
  1949.   (funcall browse-url-browser-function (widget-value widget)))
  1950.  
  1951. ;;; The `function-link' Widget.
  1952.  
  1953. (define-widget 'function-link 'link
  1954.   "A link to an Emacs function."
  1955.   :action 'widget-function-link-action)
  1956.  
  1957. (defun widget-function-link-action (widget &optional event)
  1958.   "Show the function specified by WIDGET."
  1959.   (describe-function (widget-value widget)))
  1960.  
  1961. ;;; The `variable-link' Widget.
  1962.  
  1963. (define-widget 'variable-link 'link
  1964.   "A link to an Emacs variable."
  1965.   :action 'widget-variable-link-action)
  1966.  
  1967. (defun widget-variable-link-action (widget &optional event)
  1968.   "Show the variable specified by WIDGET."
  1969.   (describe-variable (widget-value widget)))
  1970.  
  1971. ;;; The `file-link' Widget.
  1972.  
  1973. (define-widget 'file-link 'link
  1974.   "A link to a file."
  1975.   :action 'widget-file-link-action)
  1976.  
  1977. (defun widget-file-link-action (widget &optional event)
  1978.   "Find the file specified by WIDGET."
  1979.   (find-file (widget-value widget)))
  1980.  
  1981. ;;; The `emacs-library-link' Widget.
  1982.  
  1983. (define-widget 'emacs-library-link 'link
  1984.   "A link to an Emacs Lisp library file."
  1985.   :help-echo 'widget-emacs-library-link-help-echo
  1986.   :action 'widget-emacs-library-link-action)
  1987.  
  1988. (defun widget-emacs-library-link-help-echo (widget)
  1989.   (concat "Visit " (widget-value widget)))
  1990.  
  1991. (defun widget-emacs-library-link-action (widget &optional event)
  1992.   "Find the Emacs Library file specified by WIDGET."
  1993.   (find-file (locate-library (widget-value widget))))
  1994.  
  1995. ;;; The `emacs-commentary-link' Widget.
  1996.  
  1997. (define-widget 'emacs-commentary-link 'link
  1998.   "A link to Commentary in an Emacs Lisp library file."
  1999.   :action 'widget-emacs-commentary-link-action)
  2000.  
  2001. (defun widget-emacs-commentary-link-action (widget &optional event)
  2002.   "Find the Commentary section of the Emacs file specified by WIDGET."
  2003.   (finder-commentary (widget-value widget)))
  2004.  
  2005. ;;; The `editable-field' Widget.
  2006.  
  2007. (define-widget 'editable-field 'default
  2008.   "An editable text field."
  2009.   :convert-widget 'widget-value-convert-widget
  2010.   :keymap widget-field-keymap
  2011.   :format "%v"
  2012.   :value ""
  2013.   :prompt-internal 'widget-field-prompt-internal
  2014.   :prompt-history 'widget-field-history
  2015.   :prompt-value 'widget-field-prompt-value
  2016.   :action 'widget-field-action
  2017.   :validate 'widget-field-validate
  2018.   :valid-regexp ""
  2019.   :error "No match"
  2020.   :value-create 'widget-field-value-create
  2021.   :value-delete 'widget-field-value-delete
  2022.   :value-get 'widget-field-value-get
  2023.   :match 'widget-field-match)
  2024.  
  2025. (defvar widget-field-history nil
  2026.   "History of field minibuffer edits.")
  2027.  
  2028. (defun widget-field-prompt-internal (widget prompt initial history)
  2029.   ;; Read string for WIDGET prompting with PROMPT.
  2030.   ;; INITIAL is the initial input and HISTORY is a symbol containing
  2031.   ;; the earlier input.
  2032.   (read-string prompt initial history))
  2033.  
  2034. (defun widget-field-prompt-value (widget prompt value unbound)
  2035.   ;; Prompt for a string.
  2036.   (let ((initial (if unbound
  2037.              nil
  2038.            (cons (widget-apply widget :value-to-internal
  2039.                        value) 0)))
  2040.     (history (widget-get widget :prompt-history)))
  2041.     (let ((answer (widget-apply widget
  2042.                 :prompt-internal prompt initial history)))
  2043.       (widget-apply widget :value-to-external answer))))
  2044.  
  2045. (defvar widget-edit-functions nil)
  2046.  
  2047. (defun widget-field-action (widget &optional event)
  2048.   ;; Edit the value in the minibuffer.
  2049.   (let* ((invalid (widget-apply widget :validate))
  2050.      (prompt (concat (widget-apply widget :menu-tag-get) ": "))
  2051.      (value (unless invalid
  2052.           (widget-value widget)))
  2053.      (answer (widget-apply widget :prompt-value prompt value invalid)))
  2054.     (unless (equal value answer)
  2055.       ;; This is a hack.  We can't properly validate the widget
  2056.       ;; because validation requires the new value to be in the field.
  2057.       ;; However, widget-field-value-create will not function unless
  2058.       ;; the new value matches.  So, we check whether the thing
  2059.       ;; matches, and if it does, use either the real or a dummy error
  2060.       ;; message.
  2061.       (unless (widget-apply widget :match answer)
  2062.     (let ((error-message (or (widget-get widget :type-error)
  2063.                  "Invalid field contents")))
  2064.       (widget-put widget :error error-message)
  2065.       (error error-message)))
  2066.       (widget-value-set widget answer)
  2067.       (widget-apply widget :notify widget event)
  2068.       (widget-setup))
  2069.     (run-hook-with-args 'widget-edit-functions widget)))
  2070.  
  2071. ;(defun widget-field-action (widget &optional event)
  2072. ;  ;; Move to next field.
  2073. ;  (widget-forward 1)
  2074. ;  (run-hook-with-args 'widget-edit-functions widget))
  2075.  
  2076. (defun widget-field-validate (widget)
  2077.   ;; Valid if the content matches `:valid-regexp'.
  2078.   (save-excursion
  2079.     (let ((value (widget-apply widget :value-get))
  2080.       (regexp (widget-get widget :valid-regexp)))
  2081.       (if (string-match regexp value)
  2082.       nil
  2083.     widget))))
  2084.  
  2085. (defun widget-field-value-create (widget)
  2086.   ;; Create an editable text field.
  2087.   (let ((size (widget-get widget :size))
  2088.     (value (widget-get widget :value))
  2089.     (from (point))
  2090.     ;; This is changed to a real extent in `widget-setup'.  We
  2091.     ;; need the end points to behave differently until
  2092.     ;; `widget-setup' is called.  Should probably be replaced with
  2093.     ;; a genuine extent, but some things break, then.
  2094.     (extent (cons (make-marker) (make-marker))))
  2095.     (widget-put widget :field-extent extent)
  2096.     (insert value)
  2097.     (and size
  2098.      (< (length value) size)
  2099.      (insert-char ?\  (- size (length value))))
  2100.     (unless (memq widget widget-field-list)
  2101.       (push widget widget-field-new))
  2102.     (move-marker (cdr extent) (point))
  2103.     (set-marker-insertion-type (cdr extent) nil)
  2104.     (when (null size)
  2105.       (insert ?\n))
  2106.     (move-marker (car extent) from)
  2107.     (set-marker-insertion-type (car extent) t)))
  2108.  
  2109. (defun widget-field-value-delete (widget)
  2110.   ;; Remove the widget from the list of active editing fields.
  2111.   (setq widget-field-list (delq widget widget-field-list))
  2112.   ;; These are nil if the :format string doesn't contain `%v'.
  2113.   (let ((extent (widget-get widget :field-extent)))
  2114.     (when extent
  2115.       (detach-extent extent))))
  2116.  
  2117. (defun widget-field-value-get (widget)
  2118.   ;; Return current text in editing field.
  2119.   (let ((from (widget-field-start widget))
  2120.     (to (widget-field-end widget))
  2121.     (buffer (widget-field-buffer widget))
  2122.     (size (widget-get widget :size))
  2123.     (secret (widget-get widget :secret))
  2124.     (old (current-buffer)))
  2125.     (cond
  2126.      ((and from to)
  2127.       (set-buffer buffer)
  2128.       (while (and size
  2129.           (not (zerop size))
  2130.           (> to from)
  2131.           (eq (char-after (1- to)) ?\ ))
  2132.     (setq to (1- to)))
  2133.       (let ((result (buffer-substring-no-properties from to)))
  2134.     (when secret
  2135.       (let ((index 0))
  2136.         (while (< (+ from index) to)
  2137.           (aset result index
  2138.             (get-char-property (+ from index) 'secret))
  2139.           (incf index))))
  2140.     (set-buffer old)
  2141.     result))
  2142.      (t
  2143.       (widget-get widget :value)))))
  2144.  
  2145. (defun widget-field-match (widget value)
  2146.   ;; Match any string.
  2147.   (stringp value))
  2148.  
  2149. ;;; The `text' Widget.
  2150.  
  2151. (define-widget 'text 'editable-field
  2152.   :keymap widget-text-keymap
  2153.   "A multiline text area.")
  2154.  
  2155. ;;; The `menu-choice' Widget.
  2156.  
  2157. (define-widget 'menu-choice 'default
  2158.   "A menu of options."
  2159.   :convert-widget  'widget-types-convert-widget
  2160.   :format "%[%t%]: %v"
  2161.   :case-fold t
  2162.   :tag "choice"
  2163.   :void '(item :format "invalid (%t)\n")
  2164.   :value-create 'widget-choice-value-create
  2165.   :value-delete 'widget-children-value-delete
  2166.   :value-get 'widget-choice-value-get
  2167.   :value-inline 'widget-choice-value-inline
  2168.   :mouse-down-action 'widget-choice-mouse-down-action
  2169.   :action 'widget-choice-action
  2170.   :error "Make a choice"
  2171.   :validate 'widget-choice-validate
  2172.   :match 'widget-choice-match
  2173.   :match-inline 'widget-choice-match-inline)
  2174.  
  2175. (defun widget-choice-value-create (widget)
  2176.   ;; Insert the first choice that matches the value.
  2177.   (let ((value (widget-get widget :value))
  2178.     (args (widget-get widget :args))
  2179.     current)
  2180.     (while args
  2181.       (setq current (car args)
  2182.         args (cdr args))
  2183.       (when (widget-apply current :match value)
  2184.     (widget-put widget :children (list (widget-create-child-value
  2185.                         widget current value)))
  2186.     (widget-put widget :choice current)
  2187.     (setq args nil
  2188.           current nil)))
  2189.     (when current
  2190.       (let ((void (widget-get widget :void)))
  2191.     (widget-put widget :children (list (widget-create-child-and-convert
  2192.                         widget void :value value)))
  2193.     (widget-put widget :choice void)))))
  2194.  
  2195. (defun widget-choice-value-get (widget)
  2196.   ;; Get value of the child widget.
  2197.   (widget-value (car (widget-get widget :children))))
  2198.  
  2199. (defun widget-choice-value-inline (widget)
  2200.   ;; Get value of the child widget.
  2201.   (widget-apply (car (widget-get widget :children)) :value-inline))
  2202.  
  2203. (defcustom widget-choice-toggle nil
  2204.   "If non-nil, a binary choice will just toggle between the values.
  2205. Otherwise, the user will explicitly have to choose between the values
  2206. when he invoked the menu."
  2207.   :type 'boolean
  2208.   :group 'widgets)
  2209.  
  2210. (defun widget-choice-mouse-down-action (widget &optional event)
  2211.   ;; Return non-nil if we need a menu.
  2212.   (let ((args (widget-get widget :args))
  2213.     (old (widget-get widget :choice)))
  2214.     (cond ((not (console-on-window-system-p))
  2215.        ;; No place to pop up a menu.
  2216.        nil)
  2217.       ((< (length args) 2)
  2218.        ;; Empty or singleton list, just return the value.
  2219.        nil)
  2220.       ((> (length args) widget-menu-max-size)
  2221.        ;; Too long, prompt.
  2222.        nil)
  2223.       ((> (length args) 2)
  2224.        ;; Reasonable sized list, use menu.
  2225.        t)
  2226.       ((and widget-choice-toggle (memq old args))
  2227.        ;; We toggle.
  2228.        nil)
  2229.       (t
  2230.        ;; Ask which of the two.
  2231.        t))))
  2232.  
  2233. (defun widget-choice-action (widget &optional event)
  2234.   ;; Make a choice.
  2235.   (let ((args (widget-get widget :args))
  2236.     (old (widget-get widget :choice))
  2237.     (tag (widget-apply widget :menu-tag-get))
  2238.     (completion-ignore-case (widget-get widget :case-fold))
  2239.     current choices)
  2240.     ;; Remember old value.
  2241.     (if (and old (not (widget-apply widget :validate)))
  2242.     (let* ((external (widget-value widget))
  2243.            (internal (widget-apply old :value-to-internal external)))
  2244.       (widget-put old :value internal)))
  2245.     ;; Find new choice.
  2246.     (setq current
  2247.       (cond ((= (length args) 0)
  2248.          nil)
  2249.         ((= (length args) 1)
  2250.          (nth 0 args))
  2251.         ((and widget-choice-toggle
  2252.               (= (length args) 2)
  2253.               (memq old args))
  2254.          (if (eq old (nth 0 args))
  2255.              (nth 1 args)
  2256.            (nth 0 args)))
  2257.         (t
  2258.          (while args
  2259.            (setq current (car args)
  2260.              args (cdr args))
  2261.            (setq choices
  2262.              (cons (cons (widget-apply current :menu-tag-get)
  2263.                      current)
  2264.                    choices)))
  2265.          (widget-choose tag (reverse choices) event))))
  2266.     (when current
  2267.       (widget-value-set widget
  2268.             (widget-apply current :value-to-external
  2269.                       (widget-get current :value)))
  2270.       (widget-setup)
  2271.       (widget-apply widget :notify widget event)))
  2272.   (run-hook-with-args 'widget-edit-functions widget))
  2273.  
  2274. (defun widget-choice-validate (widget)
  2275.   ;; Valid if we have made a valid choice.
  2276.   (let ((void (widget-get widget :void))
  2277.     (choice (widget-get widget :choice))
  2278.     (child (car (widget-get widget :children))))
  2279.     (if (eq void choice)
  2280.     widget
  2281.       (widget-apply child :validate))))
  2282.  
  2283. (defun widget-choice-match (widget value)
  2284.   ;; Matches if one of the choices matches.
  2285.   (let ((args (widget-get widget :args))
  2286.     current found)
  2287.     (while (and args (not found))
  2288.       (setq current (car args)
  2289.         args (cdr args)
  2290.         found (widget-apply current :match value)))
  2291.     found))
  2292.  
  2293. (defun widget-choice-match-inline (widget values)
  2294.   ;; Matches if one of the choices matches.
  2295.   (let ((args (widget-get widget :args))
  2296.     current found)
  2297.     (while (and args (null found))
  2298.       (setq current (car args)
  2299.         args (cdr args)
  2300.         found (widget-match-inline current values)))
  2301.     found))
  2302.  
  2303. ;;; The `toggle' Widget.
  2304.  
  2305. (define-widget 'toggle 'item
  2306.   "Toggle between two states."
  2307.   :format "%[%v%]\n"
  2308.   :value-create 'widget-toggle-value-create
  2309.   :action 'widget-toggle-action
  2310.   :match (lambda (widget value) t)
  2311.   :on "on"
  2312.   :off "off")
  2313.  
  2314. (defun widget-toggle-value-create (widget)
  2315.   ;; Insert text representing the `on' and `off' states.
  2316.   (if (widget-value widget)
  2317.       (widget-glyph-insert widget
  2318.                (widget-get widget :on)
  2319.                (widget-get widget :on-glyph))
  2320.       (widget-glyph-insert widget
  2321.                (widget-get widget :off)
  2322.                (widget-get widget :off-glyph))))
  2323.  
  2324. (defun widget-toggle-action (widget &optional event)
  2325.   ;; Toggle value.
  2326.   (widget-value-set widget (not (widget-value widget)))
  2327.   (widget-apply widget :notify widget event)
  2328.   (run-hook-with-args 'widget-edit-functions widget))
  2329.  
  2330. ;;; The `checkbox' Widget.
  2331.  
  2332. (define-widget 'checkbox 'toggle
  2333.   "A checkbox toggle."
  2334.   :button-suffix ""
  2335.   :button-prefix ""
  2336.   :format "%[%v%]"
  2337.   :on "[X]"
  2338.   :on-glyph "check1"
  2339.   :off "[ ]"
  2340.   :off-glyph "check0"
  2341.   :action 'widget-checkbox-action)
  2342.  
  2343. (defun widget-checkbox-action (widget &optional event)
  2344.   "Toggle checkbox, notify parent, and set active state of sibling."
  2345.   (widget-toggle-action widget event)
  2346.   (let ((sibling (widget-get-sibling widget)))
  2347.     (when sibling
  2348.       (if (widget-value widget)
  2349.       (widget-apply sibling :activate)
  2350.     (widget-apply sibling :deactivate)))))
  2351.  
  2352. ;;; The `checklist' Widget.
  2353.  
  2354. (define-widget 'checklist 'default
  2355.   "A multiple choice widget."
  2356.   :convert-widget 'widget-types-convert-widget
  2357.   :format "%v"
  2358.   :offset 4
  2359.   :entry-format "%b %v"
  2360.   :menu-tag "checklist"
  2361.   :greedy nil
  2362.   :value-create 'widget-checklist-value-create
  2363.   :value-delete 'widget-children-value-delete
  2364.   :value-get 'widget-checklist-value-get
  2365.   :validate 'widget-checklist-validate
  2366.   :match 'widget-checklist-match
  2367.   :match-inline 'widget-checklist-match-inline)
  2368.  
  2369. (defun widget-checklist-value-create (widget)
  2370.   ;; Insert all values
  2371.   (let ((alist (widget-checklist-match-find widget (widget-get widget :value)))
  2372.     (args (widget-get widget :args)))
  2373.     (while args
  2374.       (widget-checklist-add-item widget (car args) (assq (car args) alist))
  2375.       (setq args (cdr args)))
  2376.     (widget-put widget :children (nreverse (widget-get widget :children)))))
  2377.  
  2378. (defun widget-checklist-add-item (widget type chosen)
  2379.   ;; Create checklist item in WIDGET of type TYPE.
  2380.   ;; If the item is checked, CHOSEN is a cons whose cdr is the value.
  2381.   (and (eq (preceding-char) ?\n)
  2382.        (widget-get widget :indent)
  2383.        (insert-char ?\  (widget-get widget :indent)))
  2384.   (widget-specify-insert
  2385.    (let* ((children (widget-get widget :children))
  2386.       (buttons (widget-get widget :buttons))
  2387.       (button-args (or (widget-get type :sibling-args)
  2388.                (widget-get widget :button-args)))
  2389.       (from (point))
  2390.       child button)
  2391.      (insert (widget-get widget :entry-format))
  2392.      (goto-char from)
  2393.      ;; Parse % escapes in format.
  2394.      (while (re-search-forward "%\\([bv%]\\)" nil t)
  2395.        (let ((escape (aref (match-string 1) 0)))
  2396.      (replace-match "" t t)
  2397.      (cond ((eq escape ?%)
  2398.         (insert "%"))
  2399.            ((eq escape ?b)
  2400.         (setq button (apply 'widget-create-child-and-convert
  2401.                     widget 'checkbox
  2402.                     :value (not (null chosen))
  2403.                     button-args)))
  2404.            ((eq escape ?v)
  2405.         (setq child
  2406.               (cond ((not chosen)
  2407.                  (let ((child (widget-create-child widget type)))
  2408.                    (widget-apply child :deactivate)
  2409.                    child))
  2410.                 ((widget-get type :inline)
  2411.                  (widget-create-child-value
  2412.                   widget type (cdr chosen)))
  2413.                 (t
  2414.                  (widget-create-child-value
  2415.                   widget type (car (cdr chosen)))))))
  2416.            (t
  2417.         (error "Unknown escape `%c'" escape)))))
  2418.      ;; Update properties.
  2419.      (and button child (widget-put child :button button))
  2420.      (and button (widget-put widget :buttons (cons button buttons)))
  2421.      (and child (widget-put widget :children (cons child children))))))
  2422.  
  2423. (defun widget-checklist-match (widget values)
  2424.   ;; All values must match a type in the checklist.
  2425.   (and (listp values)
  2426.        (null (cdr (widget-checklist-match-inline widget values)))))
  2427.  
  2428. (defun widget-checklist-match-inline (widget values)
  2429.   ;; Find the values which match a type in the checklist.
  2430.   (let ((greedy (widget-get widget :greedy))
  2431.     (args (copy-sequence (widget-get widget :args)))
  2432.     found rest)
  2433.     (while values
  2434.       (let ((answer (widget-checklist-match-up args values)))
  2435.     (cond (answer
  2436.            (let ((vals (widget-match-inline answer values)))
  2437.          (setq found (append found (car vals))
  2438.                values (cdr vals)
  2439.                args (delq answer args))))
  2440.           (greedy
  2441.            (setq rest (append rest (list (car values)))
  2442.              values (cdr values)))
  2443.           (t
  2444.            (setq rest (append rest values)
  2445.              values nil)))))
  2446.     (cons found rest)))
  2447.  
  2448. (defun widget-checklist-match-find (widget vals)
  2449.   ;; Find the vals which match a type in the checklist.
  2450.   ;; Return an alist of (TYPE MATCH).
  2451.   (let ((greedy (widget-get widget :greedy))
  2452.     (args (copy-sequence (widget-get widget :args)))
  2453.     found)
  2454.     (while vals
  2455.       (let ((answer (widget-checklist-match-up args vals)))
  2456.     (cond (answer
  2457.            (let ((match (widget-match-inline answer vals)))
  2458.          (setq found (cons (cons answer (car match)) found)
  2459.                vals (cdr match)
  2460.                args (delq answer args))))
  2461.           (greedy
  2462.            (setq vals (cdr vals)))
  2463.           (t
  2464.            (setq vals nil)))))
  2465.     found))
  2466.  
  2467. (defun widget-checklist-match-up (args vals)
  2468.   ;; Rerturn the first type from ARGS that matches VALS.
  2469.   (let (current found)
  2470.     (while (and args (null found))
  2471.       (setq current (car args)
  2472.         args (cdr args)
  2473.         found (widget-match-inline current vals)))
  2474.     (if found
  2475.     current
  2476.       nil)))
  2477.  
  2478. (defun widget-checklist-value-get (widget)
  2479.   ;; The values of all selected items.
  2480.   (let ((children (widget-get widget :children))
  2481.     child result)
  2482.     (while children
  2483.       (setq child (car children)
  2484.         children (cdr children))
  2485.       (if (widget-value (widget-get child :button))
  2486.       (setq result (append result (widget-apply child :value-inline)))))
  2487.     result))
  2488.  
  2489. (defun widget-checklist-validate (widget)
  2490.   ;; Ticked chilren must be valid.
  2491.   (let ((children (widget-get widget :children))
  2492.     child button found)
  2493.     (while (and children (not found))
  2494.       (setq child (car children)
  2495.         children (cdr children)
  2496.         button (widget-get child :button)
  2497.         found (and (widget-value button)
  2498.                (widget-apply child :validate))))
  2499.     found))
  2500.  
  2501. ;;; The `option' Widget
  2502.  
  2503. (define-widget 'option 'checklist
  2504.   "An widget with an optional item."
  2505.   :inline t)
  2506.  
  2507. ;;; The `choice-item' Widget.
  2508.  
  2509. (define-widget 'choice-item 'item
  2510.   "Button items that delegate action events to their parents."
  2511.   :action 'widget-parent-action
  2512.   :format "%[%t%] \n")
  2513.  
  2514. ;;; The `radio-button' Widget.
  2515.  
  2516. (define-widget 'radio-button 'toggle
  2517.   "A radio button for use in the `radio' widget."
  2518.   :notify 'widget-radio-button-notify
  2519.   :format "%[%v%]"
  2520.   :button-suffix ""
  2521.   :button-prefix ""
  2522.   :on "(*)"
  2523.   :on-glyph '("radio1" nil "radio0")
  2524.   :off "( )"
  2525.   :off-glyph "radio0")
  2526.  
  2527. (defun widget-radio-button-notify (widget child &optional event)
  2528.   ;; Tell daddy.
  2529.   (widget-apply (widget-get widget :parent) :action widget event))
  2530.  
  2531. ;;; The `radio-button-choice' Widget.
  2532.  
  2533. (define-widget 'radio-button-choice 'default
  2534.   "Select one of multiple options."
  2535.   :convert-widget 'widget-types-convert-widget
  2536.   :offset 4
  2537.   :format "%v"
  2538.   :entry-format "%b %v"
  2539.   :menu-tag "radio"
  2540.   :value-create 'widget-radio-value-create
  2541.   :value-delete 'widget-children-value-delete
  2542.   :value-get 'widget-radio-value-get
  2543.   :value-inline 'widget-radio-value-inline
  2544.   :value-set 'widget-radio-value-set
  2545.   :error "You must push one of the buttons"
  2546.   :validate 'widget-radio-validate
  2547.   :match 'widget-choice-match
  2548.   :match-inline 'widget-choice-match-inline
  2549.   :action 'widget-radio-action)
  2550.  
  2551. (defun widget-radio-value-create (widget)
  2552.   ;; Insert all values
  2553.   (let ((args (widget-get widget :args))
  2554.     arg)
  2555.     (while args
  2556.       (setq arg (car args)
  2557.         args (cdr args))
  2558.       (widget-radio-add-item widget arg))))
  2559.  
  2560. (defun widget-radio-add-item (widget type)
  2561.   "Add to radio widget WIDGET a new radio button item of type TYPE."
  2562.   ;; (setq type (widget-convert type))
  2563.   (and (eq (preceding-char) ?\n)
  2564.        (widget-get widget :indent)
  2565.        (insert-char ?\  (widget-get widget :indent)))
  2566.   (widget-specify-insert
  2567.    (let* ((value (widget-get widget :value))
  2568.       (children (widget-get widget :children))
  2569.       (buttons (widget-get widget :buttons))
  2570.       (button-args (or (widget-get type :sibling-args)
  2571.                (widget-get widget :button-args)))
  2572.       (from (point))
  2573.       (chosen (and (null (widget-get widget :choice))
  2574.                (widget-apply type :match value)))
  2575.       child button)
  2576.      (insert (widget-get widget :entry-format))
  2577.      (goto-char from)
  2578.      ;; Parse % escapes in format.
  2579.      (while (re-search-forward "%\\([bv%]\\)" nil t)
  2580.        (let ((escape (aref (match-string 1) 0)))
  2581.      (replace-match "" t t)
  2582.      (cond ((eq escape ?%)
  2583.         (insert "%"))
  2584.            ((eq escape ?b)
  2585.         (setq button (apply 'widget-create-child-and-convert
  2586.                     widget 'radio-button
  2587.                     :value (not (null chosen))
  2588.                     button-args)))
  2589.            ((eq escape ?v)
  2590.         (setq child (if chosen
  2591.                 (widget-create-child-value
  2592.                  widget type value)
  2593.                   (widget-create-child widget type)))
  2594.         (unless chosen
  2595.           (widget-apply child :deactivate)))
  2596.            (t
  2597.         (error "Unknown escape `%c'" escape)))))
  2598.      ;; Update properties.
  2599.      (when chosen
  2600.        (widget-put widget :choice type))
  2601.      (when button
  2602.        (widget-put child :button button)
  2603.        (widget-put widget :buttons (nconc buttons (list button))))
  2604.      (when child
  2605.        (widget-put widget :children (nconc children (list child))))
  2606.      child)))
  2607.  
  2608. (defun widget-radio-value-get (widget)
  2609.   ;; Get value of the child widget.
  2610.   (let ((chosen (widget-radio-chosen widget)))
  2611.     (and chosen (widget-value chosen))))
  2612.  
  2613. (defun widget-radio-chosen (widget)
  2614.   "Return the widget representing the chosen radio button."
  2615.   (let ((children (widget-get widget :children))
  2616.     current found)
  2617.     (while children
  2618.       (setq current (car children)
  2619.         children (cdr children))
  2620.       (let* ((button (widget-get current :button))
  2621.          (value (widget-apply button :value-get)))
  2622.     (when value
  2623.       (setq found current
  2624.         children nil))))
  2625.     found))
  2626.  
  2627. (defun widget-radio-value-inline (widget)
  2628.   ;; Get value of the child widget.
  2629.   (let ((children (widget-get widget :children))
  2630.     current found)
  2631.     (while children
  2632.       (setq current (car children)
  2633.         children (cdr children))
  2634.       (let* ((button (widget-get current :button))
  2635.          (value (widget-apply button :value-get)))
  2636.     (when value
  2637.       (setq found (widget-apply current :value-inline)
  2638.         children nil))))
  2639.     found))
  2640.  
  2641. (defun widget-radio-value-set (widget value)
  2642.   ;; We can't just delete and recreate a radio widget, since children
  2643.   ;; can be added after the original creation and won't be recreated
  2644.   ;; by `:create'.
  2645.   (let ((children (widget-get widget :children))
  2646.     current found)
  2647.     (while children
  2648.       (setq current (car children)
  2649.         children (cdr children))
  2650.       (let* ((button (widget-get current :button))
  2651.          (match (and (not found)
  2652.              (widget-apply current :match value))))
  2653.     (widget-value-set button match)
  2654.     (if match
  2655.         (progn
  2656.           (widget-value-set current value)
  2657.           (widget-apply current :activate))
  2658.       (widget-apply current :deactivate))
  2659.     (setq found (or found match))))))
  2660.  
  2661. (defun widget-radio-validate (widget)
  2662.   ;; Valid if we have made a valid choice.
  2663.   (let ((children (widget-get widget :children))
  2664.     current found button)
  2665.     (while (and children (not found))
  2666.       (setq current (car children)
  2667.         children (cdr children)
  2668.         button (widget-get current :button)
  2669.         found (widget-apply button :value-get)))
  2670.     (if found
  2671.     (widget-apply current :validate)
  2672.       widget)))
  2673.  
  2674. (defun widget-radio-action (widget child event)
  2675.   ;; Check if a radio button was pressed.
  2676.   (let ((children (widget-get widget :children))
  2677.     (buttons (widget-get widget :buttons))
  2678.     current)
  2679.     (when (memq child buttons)
  2680.       (while children
  2681.     (setq current (car children)
  2682.           children (cdr children))
  2683.     (let* ((button (widget-get current :button)))
  2684.       (cond ((eq child button)
  2685.          (widget-value-set button t)
  2686.          (widget-apply current :activate))
  2687.         ((widget-value button)
  2688.          (widget-value-set button nil)
  2689.          (widget-apply current :deactivate)))))))
  2690.   ;; Pass notification to parent.
  2691.   (widget-apply widget :notify child event))
  2692.  
  2693. ;;; The `insert-button' Widget.
  2694.  
  2695. (define-widget 'insert-button 'push-button
  2696.   "An insert button for the `editable-list' widget."
  2697.   :tag "INS"
  2698.   :help-echo "Insert a new item into the list at this position"
  2699.   :action 'widget-insert-button-action)
  2700.  
  2701. (defun widget-insert-button-action (widget &optional event)
  2702.   ;; Ask the parent to insert a new item.
  2703.   (widget-apply (widget-get widget :parent)
  2704.         :insert-before (widget-get widget :widget)))
  2705.  
  2706. ;;; The `delete-button' Widget.
  2707.  
  2708. (define-widget 'delete-button 'push-button
  2709.   "A delete button for the `editable-list' widget."
  2710.   :tag "DEL"
  2711.   :help-echo "Delete this item from the list"
  2712.   :action 'widget-delete-button-action)
  2713.  
  2714. (defun widget-delete-button-action (widget &optional event)
  2715.   ;; Ask the parent to insert a new item.
  2716.   (widget-apply (widget-get widget :parent)
  2717.         :delete-at (widget-get widget :widget)))
  2718.  
  2719. ;;; The `editable-list' Widget.
  2720.  
  2721. (defcustom widget-editable-list-gui nil
  2722.   "If non nil, use GUI push-buttons in editable list when available."
  2723.   :type 'boolean
  2724.   :group 'widgets)
  2725.  
  2726. (define-widget 'editable-list 'default
  2727.   "A variable list of widgets of the same type."
  2728.   :convert-widget 'widget-types-convert-widget
  2729.   :offset 12
  2730.   :format "%v%i\n"
  2731.   :format-handler 'widget-editable-list-format-handler
  2732.   :entry-format "%i %d %v"
  2733.   :menu-tag "editable-list"
  2734.   :value-create 'widget-editable-list-value-create
  2735.   :value-delete 'widget-children-value-delete
  2736.   :value-get 'widget-editable-list-value-get
  2737.   :validate 'widget-children-validate
  2738.   :match 'widget-editable-list-match
  2739.   :match-inline 'widget-editable-list-match-inline
  2740.   :insert-before 'widget-editable-list-insert-before
  2741.   :delete-at 'widget-editable-list-delete-at)
  2742.  
  2743. (defun widget-editable-list-format-handler (widget escape)
  2744.   ;; We recognize the insert button.
  2745.   (let ((widget-push-button-gui widget-editable-list-gui))
  2746.     (cond ((eq escape ?i)
  2747.        (and (widget-get widget :indent)
  2748.         (insert-char ?\  (widget-get widget :indent)))
  2749.        (apply 'widget-create-child-and-convert
  2750.           widget 'insert-button
  2751.           (widget-get widget :append-button-args)))
  2752.       (t
  2753.        (widget-default-format-handler widget escape)))))
  2754.  
  2755. (defun widget-editable-list-value-create (widget)
  2756.   ;; Insert all values
  2757.   (let* ((value (widget-get widget :value))
  2758.      (type (nth 0 (widget-get widget :args)))
  2759.      (inlinep (widget-get type :inline))
  2760.      children)
  2761.     (widget-put widget :value-pos (copy-marker (point)))
  2762.     (set-marker-insertion-type (widget-get widget :value-pos) t)
  2763.     (while value
  2764.       (let ((answer (widget-match-inline type value)))
  2765.     (if answer
  2766.         (setq children (cons (widget-editable-list-entry-create
  2767.                   widget
  2768.                   (if inlinep
  2769.                       (car answer)
  2770.                     (car (car answer)))
  2771.                   t)
  2772.                  children)
  2773.           value (cdr answer))
  2774.       (setq value nil))))
  2775.     (widget-put widget :children (nreverse children))))
  2776.  
  2777. (defun widget-editable-list-value-get (widget)
  2778.   ;; Get value of the child widget.
  2779.   (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
  2780.              (widget-get widget :children))))
  2781.  
  2782. (defun widget-editable-list-match (widget value)
  2783.   ;; Value must be a list and all the members must match the type.
  2784.   (and (listp value)
  2785.        (null (cdr (widget-editable-list-match-inline widget value)))))
  2786.  
  2787. (defun widget-editable-list-match-inline (widget value)
  2788.   (let ((type (nth 0 (widget-get widget :args)))
  2789.     (ok t)
  2790.     found)
  2791.     (while (and value ok)
  2792.       (let ((answer (widget-match-inline type value)))
  2793.     (if answer
  2794.         (setq found (append found (car answer))
  2795.           value (cdr answer))
  2796.       (setq ok nil))))
  2797.     (cons found value)))
  2798.  
  2799. (defun widget-editable-list-insert-before (widget before)
  2800.   ;; Insert a new child in the list of children.
  2801.   (save-excursion
  2802.     (let ((children (widget-get widget :children))
  2803.       (inhibit-read-only t)
  2804.       before-change-functions
  2805.       after-change-functions)
  2806.       (cond (before
  2807.          (goto-char (widget-get before :entry-from)))
  2808.         (t
  2809.          (goto-char (widget-get widget :value-pos))))
  2810.       (let ((child (widget-editable-list-entry-create
  2811.             widget nil nil)))
  2812.     (when (< (widget-get child :entry-from) (widget-get widget :from))
  2813.       (set-marker (widget-get widget :from)
  2814.               (widget-get child :entry-from)))
  2815.     (if (eq (car children) before)
  2816.         (widget-put widget :children (cons child children))
  2817.       (while (not (eq (car (cdr children)) before))
  2818.         (setq children (cdr children)))
  2819.       (setcdr children (cons child (cdr children)))))))
  2820.   (widget-setup)
  2821.   (widget-apply widget :notify widget))
  2822.  
  2823. (defun widget-editable-list-delete-at (widget child)
  2824.   ;; Delete child from list of children.
  2825.   (save-excursion
  2826.     (let ((buttons (copy-sequence (widget-get widget :buttons)))
  2827.       button
  2828.       (inhibit-read-only t)
  2829.       before-change-functions
  2830.       after-change-functions)
  2831.       (while buttons
  2832.     (setq button (car buttons)
  2833.           buttons (cdr buttons))
  2834.     (when (eq (widget-get button :widget) child)
  2835.       (widget-put widget
  2836.               :buttons (delq button (widget-get widget :buttons)))
  2837.       (widget-delete button))))
  2838.     (let ((entry-from (widget-get child :entry-from))
  2839.       (entry-to (widget-get child :entry-to))
  2840.       (inhibit-read-only t)
  2841.       before-change-functions
  2842.       after-change-functions)
  2843.       (widget-delete child)
  2844.       (delete-region entry-from entry-to)
  2845.       (set-marker entry-from nil)
  2846.       (set-marker entry-to nil))
  2847.     (widget-put widget :children (delq child (widget-get widget :children))))
  2848.   (widget-setup)
  2849.   (widget-apply widget :notify widget))
  2850.  
  2851. (defun widget-editable-list-entry-create (widget value conv)
  2852.   ;; Create a new entry to the list.
  2853.   (let ((type (nth 0 (widget-get widget :args)))
  2854.     (widget-push-button-gui widget-editable-list-gui)
  2855.     child delete insert)
  2856.     (widget-specify-insert
  2857.      (save-excursion
  2858.        (and (widget-get widget :indent)
  2859.         (insert-char ?\  (widget-get widget :indent)))
  2860.        (insert (widget-get widget :entry-format)))
  2861.      ;; Parse % escapes in format.
  2862.      (while (re-search-forward "%\\(.\\)" nil t)
  2863.        (let ((escape (aref (match-string 1) 0)))
  2864.      (replace-match "" t t)
  2865.      (cond ((eq escape ?%)
  2866.         (insert "%"))
  2867.            ((eq escape ?i)
  2868.         (setq insert (apply 'widget-create-child-and-convert
  2869.                     widget 'insert-button
  2870.                     (widget-get widget :insert-button-args))))
  2871.            ((eq escape ?d)
  2872.         (setq delete (apply 'widget-create-child-and-convert
  2873.                     widget 'delete-button
  2874.                     (widget-get widget :delete-button-args))))
  2875.            ((eq escape ?v)
  2876.         (if conv
  2877.             (setq child (widget-create-child-value
  2878.                  widget type value))
  2879.           (setq child (widget-create-child widget type))))
  2880.            (t
  2881.         (error "Unknown escape `%c'" escape)))))
  2882.      (widget-put widget
  2883.          :buttons (cons delete
  2884.                 (cons insert
  2885.                       (widget-get widget :buttons))))
  2886.      (let ((entry-from (copy-marker (point-min)))
  2887.        (entry-to (copy-marker (point-max))))
  2888.        (set-marker-insertion-type entry-from t)
  2889.        (set-marker-insertion-type entry-to nil)
  2890.        (widget-put child :entry-from entry-from)
  2891.        (widget-put child :entry-to entry-to)))
  2892.     (widget-put insert :widget child)
  2893.     (widget-put delete :widget child)
  2894.     child))
  2895.  
  2896. ;;; The `group' Widget.
  2897.  
  2898. (define-widget 'group 'default
  2899.   "A widget which group other widgets inside."
  2900.   :convert-widget 'widget-types-convert-widget
  2901.   :format "%v"
  2902.   :value-create 'widget-group-value-create
  2903.   :value-delete 'widget-children-value-delete
  2904.   :value-get 'widget-editable-list-value-get
  2905.   :validate 'widget-children-validate
  2906.   :match 'widget-group-match
  2907.   :match-inline 'widget-group-match-inline)
  2908.  
  2909. (defun widget-group-value-create (widget)
  2910.   ;; Create each component.
  2911.   (let ((args (widget-get widget :args))
  2912.     (value (widget-get widget :value))
  2913.     arg answer children)
  2914.     (while args
  2915.       (setq arg (car args)
  2916.         args (cdr args)
  2917.         answer (widget-match-inline arg value)
  2918.         value (cdr answer))
  2919.       (and (eq (preceding-char) ?\n)
  2920.        (widget-get widget :indent)
  2921.        (insert-char ?\  (widget-get widget :indent)))
  2922.       (push (cond ((null answer)
  2923.            (widget-create-child widget arg))
  2924.           ((widget-get arg :inline)
  2925.            (widget-create-child-value widget arg  (car answer)))
  2926.           (t
  2927.            (widget-create-child-value widget arg  (car (car answer)))))
  2928.         children))
  2929.     (widget-put widget :children (nreverse children))))
  2930.  
  2931. (defun widget-group-match (widget values)
  2932.   ;; Match if the components match.
  2933.   (and (listp values)
  2934.        (let ((match (widget-group-match-inline widget values)))
  2935.      (and match (null (cdr match))))))
  2936.  
  2937. (defun widget-group-match-inline (widget vals)
  2938.   ;; Match if the components match.
  2939.   (let ((args (widget-get widget :args))
  2940.     argument answer found)
  2941.     (while args
  2942.       (setq argument (car args)
  2943.         args (cdr args)
  2944.         answer (widget-match-inline argument vals))
  2945.       (if answer
  2946.       (setq vals (cdr answer)
  2947.         found (append found (car answer)))
  2948.     (setq vals nil
  2949.           args nil)))
  2950.     (if answer
  2951.     (cons found vals)
  2952.       nil)))
  2953.  
  2954. ;;; The `visibility' Widget.
  2955.  
  2956. (define-widget 'visibility 'item
  2957.   "An indicator and manipulator for hidden items."
  2958.   :format "%[%v%]"
  2959.   :button-prefix ""
  2960.   :button-suffix ""
  2961.   :on "Hide"
  2962.   :off "Show"
  2963.   :value-create 'widget-visibility-value-create
  2964.   :action 'widget-toggle-action
  2965.   :match (lambda (widget value) t))
  2966.  
  2967. (defun widget-visibility-value-create (widget)
  2968.   ;; Insert text representing the `on' and `off' states.
  2969.   (let ((on (widget-get widget :on))
  2970.     (off (widget-get widget :off)))
  2971.     (if on
  2972.     (setq on (concat widget-push-button-prefix
  2973.              on
  2974.              widget-push-button-suffix))
  2975.       (setq on ""))
  2976.     (if off
  2977.     (setq off (concat widget-push-button-prefix
  2978.               off
  2979.               widget-push-button-suffix))
  2980.       (setq off ""))
  2981.     (if (widget-value widget)
  2982.     (widget-glyph-insert widget on '("down" "down-pushed"))
  2983.       (widget-glyph-insert widget off '("right" "right-pushed")))))
  2984.  
  2985. ;;; The `documentation-link' Widget.
  2986. ;;
  2987. ;; This is a helper widget for `documentation-string'.
  2988.  
  2989. (define-widget 'documentation-link 'link
  2990.   "Link type used in documentation strings."
  2991.   :tab-order -1
  2992.   :help-echo 'widget-documentation-link-echo-help
  2993.   :action 'widget-documentation-link-action)
  2994.  
  2995. (defun widget-documentation-link-echo-help (widget)
  2996.   "Tell what this link will describe."
  2997.   (concat "Describe the `" (widget-get widget :value) "' symbol."))
  2998.  
  2999. (defun widget-documentation-link-action (widget &optional event)
  3000.   "Display documentation for WIDGET's value.  Ignore optional argument EVENT."
  3001.   (let* ((string (widget-get widget :value))
  3002.      (symbol (intern string)))
  3003.     (if (and (fboundp symbol) (boundp symbol))
  3004.     ;; If there are two doc strings, give the user a way to pick one.
  3005.     (apropos (concat "\\`" (regexp-quote string) "\\'"))
  3006.       (if (fboundp symbol)
  3007.       (describe-function symbol)
  3008.     (describe-variable symbol)))))
  3009.  
  3010. (defcustom widget-documentation-links t
  3011.   "Add hyperlinks to documentation strings when non-nil."
  3012.   :type 'boolean
  3013.   :group 'widget-documentation)
  3014.  
  3015. (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
  3016.   "Regexp for matching potential links in documentation strings.
  3017. The first group should be the link itself."
  3018.   :type 'regexp
  3019.   :group 'widget-documentation)
  3020.  
  3021. (defcustom widget-documentation-link-p 'intern-soft
  3022.   "Predicate used to test if a string is useful as a link.
  3023. The value should be a function.  The function will be called one
  3024. argument, a string, and should return non-nil if there should be a
  3025. link for that string."
  3026.   :type 'function
  3027.   :options '(widget-documentation-link-p)
  3028.   :group 'widget-documentation)
  3029.  
  3030. (defcustom widget-documentation-link-type 'documentation-link
  3031.   "Widget type used for links in documentation strings."
  3032.   :type 'symbol
  3033.   :group 'widget-documentation)
  3034.  
  3035. (defun widget-documentation-link-add (widget from to)
  3036.   (widget-specify-doc widget from to)
  3037.   (when widget-documentation-links
  3038.     (let ((regexp widget-documentation-link-regexp)
  3039.       (predicate widget-documentation-link-p)
  3040.       (type widget-documentation-link-type)
  3041.       (buttons (widget-get widget :buttons)))
  3042.       (save-excursion
  3043.     (goto-char from)
  3044.     (while (re-search-forward regexp to t)
  3045.       (let ((name (match-string 1))
  3046.         (begin (match-beginning 1))
  3047.         (end (match-end 1)))
  3048.         (when (funcall predicate name)
  3049.           (push (widget-convert-button type begin end :value name)
  3050.             buttons)))))
  3051.       (widget-put widget :buttons buttons)))
  3052.   (let ((indent (widget-get widget :indent)))
  3053.     (when (and indent (not (zerop indent)))
  3054.       (save-excursion
  3055.     (save-restriction
  3056.       (narrow-to-region from to)
  3057.       (goto-char (point-min))
  3058.       (while (search-forward "\n" nil t)
  3059.         (insert-char ?\  indent)))))))
  3060.  
  3061. ;;; The `documentation-string' Widget.
  3062.  
  3063. (define-widget 'documentation-string 'item
  3064.   "A documentation string."
  3065.   :format "%v"
  3066.   :action 'widget-documentation-string-action
  3067.   :value-delete 'widget-children-value-delete
  3068.   :value-create 'widget-documentation-string-value-create)
  3069.  
  3070. (defun widget-documentation-string-value-create (widget)
  3071.   ;; Insert documentation string.
  3072.   (let ((doc (widget-value widget))
  3073.     (indent (widget-get widget :indent))
  3074.     (shown (widget-get (widget-get widget :parent) :documentation-shown))
  3075.     (start (point)))
  3076.     (if (string-match "\n" doc)
  3077.     (let ((before (substring doc 0 (match-beginning 0)))
  3078.           (after (substring doc (match-beginning 0)))
  3079.           buttons)
  3080.       (insert before " ")
  3081.       (widget-documentation-link-add widget start (point))
  3082.       (push (widget-create-child-and-convert
  3083.          widget 'visibility
  3084.          :help-echo (lambda (widget)
  3085.                   (concat
  3086.                    (if (widget-value widget)
  3087.                    "Hide" "Show")
  3088.                    " the rest of the documentation"))
  3089.          :off "More"
  3090.          :action 'widget-parent-action
  3091.          shown)
  3092.         buttons)
  3093.       (when shown
  3094.         (setq start (point))
  3095.         (when indent
  3096.           (insert-char ?\  indent))
  3097.         (insert after)
  3098.         (widget-documentation-link-add widget start (point)))
  3099.       (widget-put widget :buttons buttons))
  3100.       (insert doc)
  3101.       (widget-documentation-link-add widget start (point))))
  3102.   (insert "\n"))
  3103.  
  3104. (defun widget-documentation-string-action (widget &rest ignore)
  3105.   ;; Toggle documentation.
  3106.   (let ((parent (widget-get widget :parent)))
  3107.     (widget-put parent :documentation-shown
  3108.         (not (widget-get parent :documentation-shown))))
  3109.   ;; Redraw.
  3110.   (widget-value-set widget (widget-value widget)))
  3111.  
  3112. ;;; The Sexp Widgets.
  3113.  
  3114. (define-widget 'const 'item
  3115.   "An immutable sexp."
  3116.   :prompt-value 'widget-const-prompt-value
  3117.   :format "%t\n%d")
  3118.  
  3119. (defun widget-const-prompt-value (widget prompt value unbound)
  3120.   ;; Return the value of the const.
  3121.   (widget-value widget))
  3122.  
  3123. (define-widget 'function-item 'const
  3124.   "An immutable function name."
  3125.   :format "%v\n%h"
  3126.   :documentation-property (lambda (symbol)
  3127.                 (condition-case nil
  3128.                 (documentation symbol t)
  3129.                   (error nil))))
  3130.  
  3131. (define-widget 'variable-item 'const
  3132.   "An immutable variable name."
  3133.   :format "%v\n%h"
  3134.   :documentation-property 'variable-documentation)
  3135.  
  3136. (defvar widget-string-prompt-value-history nil
  3137.   "History of input to `widget-string-prompt-value'.")
  3138.  
  3139. (define-widget 'string 'editable-field
  3140.   "A string"
  3141.   :tag "String"
  3142.   :format "%{%t%}: %v"
  3143.   :complete-function 'ispell-complete-word
  3144.   :prompt-history 'widget-string-prompt-value-history)
  3145.  
  3146. (define-widget 'regexp 'string
  3147.   "A regular expression."
  3148.   :match 'widget-regexp-match
  3149.   :validate 'widget-regexp-validate
  3150.   ;; Doesn't work well with terminating newline.
  3151.   ;; :value-face 'widget-single-line-field-face
  3152.   :tag "Regexp")
  3153.  
  3154. (defun widget-regexp-match (widget value)
  3155.   ;; Match valid regexps.
  3156.   (and (stringp value)
  3157.        (condition-case nil
  3158.        (prog1 t
  3159.          (string-match value ""))
  3160.      (error nil))))
  3161.  
  3162. (defun widget-regexp-validate (widget)
  3163.   "Check that the value of WIDGET is a valid regexp."
  3164.   (let ((value (widget-value widget)))
  3165.     (condition-case data
  3166.     (prog1 nil
  3167.       (string-match value ""))
  3168.       (error (widget-put widget :error (error-message-string data))
  3169.          widget))))
  3170.  
  3171. (define-widget 'file 'string
  3172.   "A file widget.
  3173. It will read a file name from the minibuffer when invoked."
  3174.   :complete-function 'widget-file-complete
  3175.   :prompt-value 'widget-file-prompt-value
  3176.   :format "%{%t%}: %v"
  3177.   ;; Doesn't work well with terminating newline.
  3178.   ;; :value-face 'widget-single-line-field-face
  3179.   :tag "File")
  3180.  
  3181. (defun widget-file-complete ()
  3182.   "Perform completion on file name preceding point."
  3183.   (interactive)
  3184.   (let* ((end (point))
  3185.      (beg (save-excursion
  3186.         (skip-chars-backward "^ ")
  3187.         (point)))
  3188.      (pattern (buffer-substring beg end))
  3189.      (name-part (file-name-nondirectory pattern))
  3190.      (directory (file-name-directory pattern))
  3191.      (completion (file-name-completion name-part directory)))
  3192.     (cond ((eq completion t))
  3193.       ((null completion)
  3194.        (message "Can't find completion for \"%s\"" pattern)
  3195.        (ding))
  3196.       ((not (string= name-part completion))
  3197.        (delete-region beg end)
  3198.        (insert (expand-file-name completion directory)))
  3199.       (t
  3200.        (message "Making completion list...")
  3201.        (let ((list (file-name-all-completions name-part directory)))
  3202.          (setq list (sort list 'string<))
  3203.          (with-output-to-temp-buffer "*Completions*"
  3204.            (display-completion-list list)))
  3205.        (message "Making completion list...%s" "done")))))
  3206.  
  3207. (defun widget-file-prompt-value (widget prompt value unbound)
  3208.   ;; Read file from minibuffer.
  3209.   (abbreviate-file-name
  3210.    (if unbound
  3211.        (read-file-name prompt)
  3212.      (let ((prompt2 (format "%s (default %s) " prompt value))
  3213.        (dir (file-name-directory value))
  3214.        (file (file-name-nondirectory value))
  3215.        (must-match (widget-get widget :must-match)))
  3216.        (read-file-name prompt2 dir nil must-match file)))))
  3217.  
  3218. ;;;(defun widget-file-action (widget &optional event)
  3219. ;;;  ;; Read a file name from the minibuffer.
  3220. ;;;  (let* ((value (widget-value widget))
  3221. ;;;     (dir (file-name-directory value))
  3222. ;;;     (file (file-name-nondirectory value))
  3223. ;;;     (menu-tag (widget-apply widget :menu-tag-get))
  3224. ;;;     (must-match (widget-get widget :must-match))
  3225. ;;;     (answer (read-file-name (concat menu-tag ": (default `" value "') ")
  3226. ;;;                 dir nil must-match file)))
  3227. ;;;    (widget-value-set widget (abbreviate-file-name answer))
  3228. ;;;    (widget-setup)
  3229. ;;;    (widget-apply widget :notify widget event)))
  3230.  
  3231. (define-widget 'directory 'file
  3232.   "A directory widget.
  3233. It will read a directory name from the minibuffer when invoked."
  3234.   :tag "Directory")
  3235.  
  3236. (defvar widget-symbol-prompt-value-history nil
  3237.   "History of input to `widget-symbol-prompt-value'.")
  3238.  
  3239. (define-widget 'symbol 'editable-field
  3240.   "A lisp symbol."
  3241.   :value nil
  3242.   :tag "Symbol"
  3243.   :format "%{%t%}: %v"
  3244.   :match (lambda (widget value) (symbolp value))
  3245.   :complete-function 'lisp-complete-symbol
  3246.   :prompt-internal 'widget-symbol-prompt-internal
  3247.   :prompt-match 'symbolp
  3248.   :prompt-history 'widget-symbol-prompt-value-history
  3249.   :value-to-internal (lambda (widget value)
  3250.                (if (symbolp value)
  3251.                (symbol-name value)
  3252.              value))
  3253.   :value-to-external (lambda (widget value)
  3254.                (if (stringp value)
  3255.                (intern value)
  3256.              value)))
  3257.  
  3258. (defun widget-symbol-prompt-internal (widget prompt initial history)
  3259.   ;; Read file from minibuffer.
  3260.   (let ((answer (completing-read prompt obarray
  3261.                  (widget-get widget :prompt-match)
  3262.                  nil initial history)))
  3263.     (if (and (stringp answer)
  3264.          (not (zerop (length answer))))
  3265.     answer
  3266.       (error "No value"))))
  3267.  
  3268. (defvar widget-function-prompt-value-history nil
  3269.   "History of input to `widget-function-prompt-value'.")
  3270.  
  3271. (define-widget 'function 'sexp
  3272.   "A lisp function."
  3273.   :complete-function 'lisp-complete-symbol
  3274.   :prompt-value 'widget-field-prompt-value
  3275.   :prompt-internal 'widget-symbol-prompt-internal
  3276.   :prompt-match 'fboundp
  3277.   :prompt-history 'widget-function-prompt-value-history
  3278.   :action 'widget-field-action
  3279.   :tag "Function")
  3280.  
  3281. (defvar widget-variable-prompt-value-history nil
  3282.   "History of input to `widget-variable-prompt-value'.")
  3283.  
  3284. (define-widget 'variable 'symbol
  3285.   ;; Should complete on variables.
  3286.   "A lisp variable."
  3287.   :prompt-match 'boundp
  3288.   :prompt-history 'widget-variable-prompt-value-history
  3289.   :tag "Variable")
  3290.  
  3291. ;; This part issues a warning when compiling without Mule.  Is there a
  3292. ;; way of shutting it up?
  3293. ;;
  3294. ;; OK, I'll simply comment the whole thing out, until someone decides
  3295. ;; to do something with it.
  3296. ;(defvar widget-coding-system-prompt-value-history nil
  3297. ;  "History of input to `widget-coding-system-prompt-value'.")
  3298.  
  3299. ;(define-widget 'coding-system 'symbol
  3300. ;  "A MULE coding-system."
  3301. ;  :format "%{%t%}: %v"
  3302. ;  :tag "Coding system"
  3303. ;  :prompt-history 'widget-coding-system-prompt-value-history
  3304. ;  :prompt-value 'widget-coding-system-prompt-value
  3305. ;  :action 'widget-coding-system-action)
  3306.  
  3307. ;(defun widget-coding-system-prompt-value (widget prompt value unbound)
  3308. ;  ;; Read coding-system from minibuffer.
  3309. ;  (intern
  3310. ;   (completing-read (format "%s (default %s) " prompt value)
  3311. ;            (mapcar (lambda (sym)
  3312. ;                  (list (symbol-name sym)))
  3313. ;                (coding-system-list)))))
  3314.  
  3315. ;(defun widget-coding-system-action (widget &optional event)
  3316. ;  ;; Read a file name from the minibuffer.
  3317. ;  (let ((answer
  3318. ;     (widget-coding-system-prompt-value
  3319. ;      widget
  3320. ;      (widget-apply widget :menu-tag-get)
  3321. ;      (widget-value widget)
  3322. ;      t)))
  3323. ;    (widget-value-set widget answer)
  3324. ;    (widget-apply widget :notify widget event)
  3325. ;    (widget-setup)))
  3326.  
  3327. (define-widget 'sexp 'editable-field
  3328.   "An arbitrary lisp expression."
  3329.   :tag "Lisp expression"
  3330.   :format "%{%t%}: %v"
  3331.   :value nil
  3332.   :validate 'widget-sexp-validate
  3333.   :match (lambda (widget value) t)
  3334.   :value-to-internal 'widget-sexp-value-to-internal
  3335.   :value-to-external (lambda (widget value) (read value))
  3336.   :prompt-history 'widget-sexp-prompt-value-history
  3337.   :prompt-value 'widget-sexp-prompt-value)
  3338.  
  3339. (defun widget-sexp-value-to-internal (widget value)
  3340.   ;; Use pp for printer representation.
  3341.   (let ((pp (if (symbolp value)
  3342.         (prin1-to-string value)
  3343.           (pp-to-string value))))
  3344.     (while (string-match "\n\\'" pp)
  3345.       (setq pp (substring pp 0 -1)))
  3346.     (if (or (string-match "\n\\'" pp)
  3347.         (> (length pp) 40))
  3348.     (concat "\n" pp)
  3349.       pp)))
  3350.  
  3351. (defun widget-sexp-validate (widget)
  3352.   ;; Valid if we can read the string and there is no junk left after it.
  3353.   (save-excursion
  3354.     (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
  3355.       (erase-buffer)
  3356.       (insert (widget-apply widget :value-get))
  3357.       (goto-char (point-min))
  3358.       (condition-case data
  3359.       (let ((value (read buffer)))
  3360.         (if (eobp)
  3361.         (if (widget-apply widget :match value)
  3362.             nil
  3363.           (widget-put widget :error (widget-get widget :type-error))
  3364.           widget)
  3365.           (widget-put widget
  3366.               :error (format "Junk at end of expression: %s"
  3367.                      (buffer-substring (point)
  3368.                                (point-max))))
  3369.           widget))
  3370.     (error (widget-put widget :error (error-message-string data))
  3371.            widget)))))
  3372.  
  3373. (defvar widget-sexp-prompt-value-history nil
  3374.   "History of input to `widget-sexp-prompt-value'.")
  3375.  
  3376. (defun widget-sexp-prompt-value (widget prompt value unbound)
  3377.   ;; Read an arbitrary sexp.
  3378.   (let ((found (read-string prompt
  3379.                 (if unbound nil (cons (prin1-to-string value) 0))
  3380.                 (widget-get widget :prompt-history))))
  3381.     (save-excursion
  3382.       (let ((buffer (set-buffer (get-buffer-create " *Widget Scratch*"))))
  3383.     (erase-buffer)
  3384.     (insert found)
  3385.     (goto-char (point-min))
  3386.     (let ((answer (read buffer)))
  3387.       (unless (eobp)
  3388.         (error "Junk at end of expression: %s"
  3389.            (buffer-substring (point) (point-max))))
  3390.       answer)))))
  3391.  
  3392. (define-widget 'restricted-sexp 'sexp
  3393.   "A Lisp expression restricted to values that match.
  3394. To use this type, you must define :match or :match-alternatives."
  3395.   :type-error "The specified value is not valid"
  3396.   :match 'widget-restricted-sexp-match
  3397.   :value-to-internal (lambda (widget value)
  3398.                (if (widget-apply widget :match value)
  3399.                (prin1-to-string value)
  3400.              value)))
  3401.  
  3402. (defun widget-restricted-sexp-match (widget value)
  3403.   (let ((alternatives (widget-get widget :match-alternatives))
  3404.     matched)
  3405.     (while (and alternatives (not matched))
  3406.       (if (cond ((functionp (car alternatives))
  3407.          (funcall (car alternatives) value))
  3408.         ((and (consp (car alternatives))
  3409.               (eq (car (car alternatives)) 'quote))
  3410.          (eq value (nth 1 (car alternatives)))))
  3411.       (setq matched t))
  3412.       (setq alternatives (cdr alternatives)))
  3413.     matched))
  3414.  
  3415. (define-widget 'integer 'restricted-sexp
  3416.   "An integer."
  3417.   :tag "Integer"
  3418.   :value 0
  3419.   :type-error "This field should contain an integer"
  3420.   :match-alternatives '(integerp))
  3421.  
  3422. (define-widget 'number 'restricted-sexp
  3423.   "A floating point number."
  3424.   :tag "Number"
  3425.   :value 0.0
  3426.   :type-error "This field should contain a number"
  3427.   :match-alternatives '(numberp))
  3428.  
  3429. (define-widget 'character 'editable-field
  3430.   "A character."
  3431.   :tag "Character"
  3432.   :value ?\0
  3433.   :format "%{%t%}: %v"
  3434.   :valid-regexp "\\`[\0-\377]\\'"
  3435.   :error "This field should contain a single character"
  3436.   :value-to-internal (lambda (widget value)
  3437.                (if (stringp value)
  3438.                value
  3439.              (char-to-string value)))
  3440.   :value-to-external (lambda (widget value)
  3441.                (if (stringp value)
  3442.                (aref value 0)
  3443.              value))
  3444.   :match (lambda (widget value)
  3445.        (characterp value)))
  3446.  
  3447. (define-widget 'list 'group
  3448.   "A lisp list."
  3449.   :tag "List"
  3450.   :format "%{%t%}:\n%v")
  3451.  
  3452. (define-widget 'vector 'group
  3453.   "A lisp vector."
  3454.   :tag "Vector"
  3455.   :format "%{%t%}:\n%v"
  3456.   :match 'widget-vector-match
  3457.   :value-to-internal (lambda (widget value) (append value nil))
  3458.   :value-to-external (lambda (widget value) (vconcat value)))
  3459.  
  3460. (defun widget-vector-match (widget value)
  3461.   (and (vectorp value)
  3462.        (widget-group-match widget
  3463.                (widget-apply widget :value-to-internal value))))
  3464.  
  3465. (define-widget 'cons 'group
  3466.   "A cons-cell."
  3467.   :tag "Cons-cell"
  3468.   :format "%{%t%}:\n%v"
  3469.   :match 'widget-cons-match
  3470.   :value-to-internal (lambda (widget value)
  3471.                (list (car value) (cdr value)))
  3472.   :value-to-external (lambda (widget value)
  3473.                (cons (car value) (cadr value))))
  3474.  
  3475. (defun widget-cons-match (widget value)
  3476.   (and (consp value)
  3477.        (widget-group-match widget
  3478.                (widget-apply widget :value-to-internal value))))
  3479.  
  3480. (define-widget 'choice 'menu-choice
  3481.   "A union of several sexp types."
  3482.   :tag "Choice"
  3483.   :format "%{%t%}: %[Value Menu%] %v"
  3484.   :button-prefix 'widget-push-button-prefix
  3485.   :button-suffix 'widget-push-button-suffix
  3486.   :prompt-value 'widget-choice-prompt-value)
  3487.  
  3488. (defun widget-choice-prompt-value (widget prompt value unbound)
  3489.   "Make a choice."
  3490.   (let ((args (widget-get widget :args))
  3491.     (completion-ignore-case (widget-get widget :case-fold))
  3492.     current choices old)
  3493.     ;; Find the first arg that match VALUE.
  3494.     (let ((look args))
  3495.       (while look
  3496.     (if (widget-apply (car look) :match value)
  3497.         (setq old (car look)
  3498.           look nil)
  3499.       (setq look (cdr look)))))
  3500.     ;; Find new choice.
  3501.     (setq current
  3502.       (cond ((= (length args) 0)
  3503.          nil)
  3504.         ((= (length args) 1)
  3505.          (nth 0 args))
  3506.         ((and (= (length args) 2)
  3507.               (memq old args))
  3508.          (if (eq old (nth 0 args))
  3509.              (nth 1 args)
  3510.            (nth 0 args)))
  3511.         (t
  3512.          (while args
  3513.            (setq current (car args)
  3514.              args (cdr args))
  3515.            (setq choices
  3516.              (cons (cons (widget-apply current :menu-tag-get)
  3517.                      current)
  3518.                    choices)))
  3519.          (let ((val (completing-read prompt choices nil t)))
  3520.            (if (stringp val)
  3521.                (let ((try (try-completion val choices)))
  3522.              (when (stringp try)
  3523.                (setq val try))
  3524.              (cdr (assoc val choices)))
  3525.              nil)))))
  3526.     (if current
  3527.     (widget-prompt-value current prompt nil t)
  3528.       value)))
  3529.  
  3530. (define-widget 'radio 'radio-button-choice
  3531.   "A union of several sexp types."
  3532.   :tag "Choice"
  3533.   :format "%{%t%}:\n%v"
  3534.   :prompt-value 'widget-choice-prompt-value)
  3535.  
  3536. (define-widget 'repeat 'editable-list
  3537.   "A variable length homogeneous list."
  3538.   :tag "Repeat"
  3539.   :format "%{%t%}:\n%v%i\n")
  3540.  
  3541. (define-widget 'set 'checklist
  3542.   "A list of members from a fixed set."
  3543.   :tag "Set"
  3544.   :format "%{%t%}:\n%v")
  3545.  
  3546. (define-widget 'boolean 'toggle
  3547.   "To be nil or non-nil, that is the question."
  3548.   :tag "Boolean"
  3549.   :prompt-value 'widget-boolean-prompt-value
  3550.   :button-prefix 'widget-push-button-prefix
  3551.   :button-suffix 'widget-push-button-suffix
  3552.   :format "%{%t%}: %[Toggle%]  %v\n"
  3553.   :on "on (non-nil)"
  3554.   :off "off (nil)")
  3555.  
  3556. (defun widget-boolean-prompt-value (widget prompt value unbound)
  3557.   ;; Toggle a boolean.
  3558.   (y-or-n-p prompt))
  3559.  
  3560. ;;; The `color' Widget.
  3561.  
  3562. (define-widget 'color 'editable-field
  3563.   "Choose a color name (with sample)."
  3564.   :format "%[%t%]: %v (%{sample%})\n"
  3565.   :size 10
  3566.   :tag "Color"
  3567.   :value "black"
  3568.   :complete 'widget-color-complete
  3569.   :sample-face-get 'widget-color-sample-face-get
  3570.   :notify 'widget-color-notify
  3571.   :action 'widget-color-action)
  3572.  
  3573. (defun widget-color-complete (widget)
  3574.   "Complete the color in WIDGET."
  3575.   (let* ((prefix (buffer-substring-no-properties (widget-field-start widget)
  3576.                          (point)))
  3577.      (list (widget-color-choice-list))
  3578.      (completion (try-completion prefix list)))
  3579.     (cond ((eq completion t)
  3580.        (message "Exact match"))
  3581.       ((null completion)
  3582.        (error "Can't find completion for \"%s\"" prefix))
  3583.       ((not (string-equal prefix completion))
  3584.        (insert (substring completion (length prefix))))
  3585.       (t
  3586.        (message "Making completion list...")
  3587.        (let ((list (all-completions prefix list nil)))
  3588.          (with-output-to-temp-buffer "*Completions*"
  3589.            (display-completion-list list)))
  3590.        (message "Making completion list...done")))))
  3591.  
  3592. (defun widget-color-sample-face-get (widget)
  3593.   (or (widget-get widget :sample-face)
  3594.       (let ((color (widget-value widget))
  3595.         (face (make-face (gensym "sample-face-") nil t)))
  3596.     ;; Use the face object, not its name, to prevent lossage if gc
  3597.     ;; happens before applying the face.
  3598.     (widget-put widget :sample-face face)
  3599.     (and color
  3600.          (not (equal color ""))
  3601.          (valid-color-name-p color)
  3602.          (set-face-foreground face color))
  3603.     face)))
  3604.  
  3605. (defvar widget-color-choice-list nil)
  3606. ;; Variable holding the possible colors.
  3607.  
  3608. (defun widget-color-choice-list ()
  3609.   (or widget-color-choice-list
  3610.       (setq widget-color-choice-list (read-color-completion-table))))
  3611.  
  3612. (defvar widget-color-history nil
  3613.   "History of entered colors")
  3614.  
  3615. (defun widget-color-action (widget &optional event)
  3616.   ;; Prompt for a color.
  3617.   (let* ((tag (widget-apply widget :menu-tag-get))
  3618.      (answer (read-color (concat tag ": "))))
  3619.     (unless (zerop (length answer))
  3620.       (widget-value-set widget answer)
  3621.       (widget-setup)
  3622.       (widget-apply widget :notify widget event))))
  3623.  
  3624. (defun widget-color-notify (widget child &optional event)
  3625.   "Update the sample, and notify the parent."
  3626.   (let* ((face (widget-apply widget :sample-face-get))
  3627.      (color (widget-value widget)))
  3628.     (if (valid-color-name-p color)
  3629.     (set-face-foreground face color)
  3630.       (remove-face-property face 'foreground)))
  3631.   (widget-default-notify widget child event))
  3632.  
  3633. ;; Is this a misnomer?
  3634. (defun widget-at (pos)
  3635.   "The button or field at POS."
  3636.   (or (get-char-property pos 'button)
  3637.       (get-char-property pos 'field)))
  3638.  
  3639. (defun widget-echo-help (pos)
  3640.   "Display the help echo for widget at POS."
  3641.   (let* ((widget (widget-at pos))
  3642.      (help-echo (and widget (widget-get widget :help-echo))))
  3643.     (and (functionp help-echo)
  3644.      (setq help-echo (funcall help-echo widget)))
  3645.     (when (stringp help-echo)
  3646.       (display-message 'help-echo help-echo))))
  3647.  
  3648. ;;; The End:
  3649.  
  3650. (provide 'wid-edit)
  3651.  
  3652. ;; wid-edit.el ends here
  3653.